home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_emacs.idb / usr / freeware / share / emacs / 19.34 / lisp / custom.el.z / custom.el
Encoding:
Text File  |  1998-10-28  |  81.9 KB  |  2,472 lines

  1. ;;; custom.el --- User friendly customization support.
  2.  
  3. ;; Copyright (C) 1995, 1996 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Per Abrahamsen <abraham@iesd.auc.dk>
  6. ;; Keywords: help
  7. ;; Version: 0.5
  8.  
  9. ;; This file is part of GNU Emacs.
  10.  
  11. ;; GNU Emacs is free software; you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 2, or (at your option)
  14. ;; any later version.
  15.  
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. ;; GNU General Public License for more details.
  20.  
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  23. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  24. ;; Boston, MA 02111-1307, USA.
  25.  
  26. ;;; Commentary:
  27.  
  28. ;; WARNING: This package is still under construction and not all of
  29. ;; the features below are implemented.
  30. ;;
  31. ;; This package provides a framework for adding user friendly
  32. ;; customization support to Emacs.  Having to do customization by
  33. ;; editing a text file in some arcane syntax is user hostile in the
  34. ;; extreme, and to most users emacs lisp definitely count as arcane.
  35. ;;
  36. ;; The intent is that authors of emacs lisp packages declare the
  37. ;; variables intended for user customization with `custom-declare'.
  38. ;; Custom can then automatically generate a customization buffer with
  39. ;; `custom-buffer-create' where the user can edit the package
  40. ;; variables in a simple and intuitive way, as well as a menu with
  41. ;; `custom-menu-create' where he can set the more commonly used
  42. ;; variables interactively.
  43. ;;
  44. ;; It is also possible to use custom for modifying the properties of
  45. ;; other objects than the package itself, by specifying extra optional
  46. ;; arguments to `custom-buffer-create'.
  47. ;;
  48. ;; Custom is inspired by OPEN LOOK property windows.
  49.  
  50. ;;; Todo:  
  51. ;;
  52. ;; - Toggle documentation in three states `none', `one-line', `full'.
  53. ;; - Function to generate an XEmacs menu from a CUSTOM.
  54. ;; - Write TeXinfo documentation.
  55. ;; - Make it possible to hide sections by clicking at the level.
  56. ;; - Declare AUC TeX variables.
  57. ;; - Declare (ding) Gnus variables.
  58. ;; - Declare Emacs variables.
  59. ;; - Implement remaining types.
  60. ;; - XEmacs port.
  61. ;; - Allow `URL', `info', and internal hypertext buttons.
  62. ;; - Support meta-variables and goal directed customization.
  63. ;; - Make it easy to declare custom types independently.
  64. ;; - Make it possible to declare default value and type for a single
  65. ;;   variable, storing the data in a symbol property.
  66. ;; - Syntactic sugar for CUSTOM declarations.
  67. ;; - Use W3 for variable documentation.
  68.  
  69. ;;; Code:
  70.  
  71. (eval-when-compile
  72.   (require 'cl))
  73.  
  74. ;;; Compatibility:
  75.  
  76. (defun custom-xmas-add-text-properties (start end props &optional object)
  77.   (add-text-properties start end props object)
  78.   (put-text-property start end 'start-open t object)
  79.   (put-text-property start end 'end-open t object))
  80.  
  81. (defun custom-xmas-put-text-property (start end prop value &optional object)
  82.   (put-text-property start end prop value object)
  83.   (put-text-property start end 'start-open t object)
  84.   (put-text-property start end 'end-open t object))
  85.  
  86. (defun custom-xmas-extent-start-open ()
  87.   (map-extents (lambda (extent arg)
  88.          (set-extent-property extent 'start-open t))
  89.            nil (point) (min (1+ (point)) (point-max))))
  90.           
  91. (if (string-match "XEmacs\\|Lucid" emacs-version)
  92.     (progn
  93.       (fset 'custom-add-text-properties 'custom-xmas-add-text-properties)
  94.       (fset 'custom-put-text-property 'custom-xmas-put-text-property)
  95.       (fset 'custom-extent-start-open 'custom-xmas-extent-start-open)
  96.       (fset 'custom-set-text-properties
  97.         (if (fboundp 'set-text-properties)
  98.         'set-text-properties))
  99.       (fset 'custom-buffer-substring-no-properties
  100.         (if (fboundp 'buffer-substring-no-properties)
  101.         'buffer-substring-no-properties
  102.           'custom-xmas-buffer-substring-no-properties)))
  103.   (fset 'custom-add-text-properties 'add-text-properties)
  104.   (fset 'custom-put-text-property 'put-text-property)
  105.   (fset 'custom-extent-start-open 'ignore)
  106.   (fset 'custom-set-text-properties 'set-text-properties)
  107.   (fset 'custom-buffer-substring-no-properties 
  108.     'buffer-substring-no-properties))
  109.  
  110. (defun custom-xmas-buffer-substring-no-properties (beg end)
  111.   "Return the text from BEG to END, without text properties, as a string."
  112.   (let ((string (buffer-substring beg end)))
  113.     (custom-set-text-properties 0 (length string) nil string)
  114.     string))
  115.  
  116. (or (fboundp 'add-to-list)
  117.     ;; Introduced in Emacs 19.29.
  118.     (defun add-to-list (list-var element)
  119.       "Add to the value of LIST-VAR the element ELEMENT if it isn't there yet.
  120. If you want to use `add-to-list' on a variable that is not defined
  121. until a certain package is loaded, you should put the call to `add-to-list'
  122. into a hook function that will be run only after loading the package.
  123. `eval-after-load' provides one way to do this.  In some cases
  124. other hooks, such as major mode hooks, can do the job."
  125.       (or (member element (symbol-value list-var))
  126.       (set list-var (cons element (symbol-value list-var))))))
  127.  
  128. (or (fboundp 'plist-get)
  129.     ;; Introduced in Emacs 19.29.
  130.     (defun plist-get (plist prop)
  131.       "Extract a value from a property list.
  132. PLIST is a property list, which is a list of the form
  133. \(PROP1 VALUE1 PROP2 VALUE2...).  This function returns the value
  134. corresponding to the given PROP, or nil if PROP is not
  135. one of the properties on the list."
  136.       (let (result)
  137.     (while plist
  138.       (if (eq (car plist) prop)
  139.           (setq result (car (cdr plist))
  140.             plist nil)
  141.         (set plist (cdr (cdr plist)))))
  142.     result)))
  143.  
  144. (or (fboundp 'plist-put)
  145.     ;; Introduced in Emacs 19.29.
  146.     (defun plist-put (plist prop val)    
  147.       "Change value in PLIST of PROP to VAL.
  148. PLIST is a property list, which is a list of the form
  149. \(PROP1 VALUE1 PROP2 VALUE2 ...).  PROP is a symbol and VAL is any object.
  150. If PROP is already a property on the list, its value is set to VAL,
  151. otherwise the new PROP VAL pair is added.  The new plist is returned;
  152. use `(setq x (plist-put x prop val))' to be sure to use the new value.
  153. The PLIST is modified by side effects."
  154.       (if (null plist)
  155.       (list prop val)
  156.     (let ((current plist))
  157.       (while current
  158.         (cond ((eq (car current) prop)
  159.            (setcar (cdr current) val)
  160.            (setq current nil))
  161.           ((null (cdr (cdr current)))
  162.            (setcdr (cdr current) (list prop val))
  163.            (setq current nil))
  164.           (t
  165.            (setq current (cdr (cdr current)))))))
  166.     plist)))
  167.  
  168. (or (fboundp 'match-string)
  169.     ;; Introduced in Emacs 19.29.
  170.     (defun match-string (num &optional string)
  171.   "Return string of text matched by last search.
  172. NUM specifies which parenthesized expression in the last regexp.
  173.  Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
  174. Zero means the entire text matched by the whole regexp or whole string.
  175. STRING should be given if the last search was by `string-match' on STRING."
  176.   (if (match-beginning num)
  177.       (if string
  178.       (substring string (match-beginning num) (match-end num))
  179.     (buffer-substring (match-beginning num) (match-end num))))))
  180.  
  181. (or (fboundp 'facep)
  182.     ;; Introduced in Emacs 19.29.
  183.     (defun facep (x)
  184.       "Return t if X is a face name or an internal face vector."
  185.       (and (or (and (fboundp 'internal-facep) (internal-facep x))
  186.            (and 
  187.         (symbolp x) 
  188.         (assq x (and (boundp 'global-face-data) global-face-data))))
  189.        t)))
  190.  
  191. ;; XEmacs and Emacs 19.29 facep does different things.
  192. (if (fboundp 'find-face)
  193.     (fset 'custom-facep 'find-face)
  194.   (fset 'custom-facep 'facep))
  195.  
  196. (if (custom-facep 'underline)
  197.     ()
  198.   ;; No underline face in XEmacs 19.12.
  199.   (and (fboundp 'make-face)
  200.        (funcall (intern "make-face") 'underline))
  201.   ;; Must avoid calling set-face-underline-p directly, because it
  202.   ;; is a defsubst in emacs19, and will make the .elc files non
  203.   ;; portable!
  204.   (or (and (fboundp 'face-differs-from-default-p)
  205.        (face-differs-from-default-p 'underline))
  206.       (and (fboundp 'set-face-underline-p)
  207.        (funcall 'set-face-underline-p 'underline t))))
  208.  
  209. (defun custom-xmas-set-text-properties (start end props &optional buffer)
  210.   (if (null buffer)
  211.       (if props
  212.       (while props
  213.         (custom-put-text-property 
  214.          start end (car props) (nth 1 props) buffer)
  215.         (setq props (nthcdr 2 props)))
  216.     (remove-text-properties start end ()))))
  217.  
  218. (or (fboundp 'event-point)
  219.     ;; Missing in Emacs 19.29.
  220.     (defun event-point (event)
  221.       "Return the character position of the given mouse-motion, button-press,
  222. or button-release event.  If the event did not occur over a window, or did
  223. not occur over text, then this returns nil.  Otherwise, it returns an index
  224. into the buffer visible in the event's window."
  225.       (posn-point (event-start event))))
  226.  
  227. (eval-when-compile
  228.   (defvar x-colors nil)
  229.   (defvar custom-button-face nil)
  230.   (defvar custom-field-uninitialized-face nil)
  231.   (defvar custom-field-invalid-face nil)
  232.   (defvar custom-field-modified-face nil)
  233.   (defvar custom-field-face nil)
  234.   (defvar custom-mouse-face nil)
  235.   (defvar custom-field-active-face nil))
  236.  
  237. ;; We can't easily check for a working intangible.
  238. (defconst intangible (if (and (boundp 'emacs-minor-version)
  239.                   (or (> emacs-major-version 19)
  240.                   (and (> emacs-major-version 18)
  241.                        (> emacs-minor-version 28))))
  242.              (setq intangible 'intangible)
  243.                (setq intangible 'intangible-if-it-had-been-working))
  244.   "The symbol making text intangible.")
  245.  
  246. (defconst rear-nonsticky (if (string-match "XEmacs" emacs-version)
  247.                  'end-open
  248.                'rear-nonsticky)
  249.   "The symbol making text properties non-sticky in the rear end.")
  250.  
  251. (defconst front-sticky (if (string-match "XEmacs" emacs-version)
  252.                'front-closed
  253.              'front-sticky)
  254.   "The symbol making text properties sticky in the front.")
  255.  
  256. (defconst mouse-face (if (string-match "XEmacs" emacs-version)
  257.              'highlight
  258.                'mouse-face)
  259.   "Symbol used for highlighting text under mouse.")
  260.  
  261. ;; Put it in the Help menu, if possible.
  262. (if (string-match "XEmacs" emacs-version)
  263.     (if (featurep 'menubar)
  264.     ;; XEmacs (disabled because it doesn't work)
  265.     (and current-menubar
  266.          (add-menu-item '("Help") "Customize..." 'customize t)))
  267.   ;; Emacs 19.28 and earlier
  268.   (global-set-key [ menu-bar help customize ]
  269.           '("Customize..." . customize))
  270.   ;; Emacs 19.29 and later
  271.   (global-set-key [ menu-bar help-menu customize ] 
  272.           '("Customize..." . customize)))
  273.  
  274. ;; XEmacs popup-menu stolen from w3.el.
  275. (defun custom-x-really-popup-menu (pos title menudesc)
  276.   "My hacked up function to do a blocking popup menu..."
  277.   (let ((echo-keystrokes 0)
  278.     event menu)
  279.     (while menudesc
  280.       (setq menu (cons (vector (car (car menudesc))
  281.                    (list (car (car menudesc))) t) menu)
  282.         menudesc (cdr menudesc)))
  283.     (setq menu (cons title menu))
  284.     (popup-menu menu)
  285.     (catch 'popup-done
  286.       (while t
  287.     (setq event (next-command-event event))
  288.     (cond ((and (misc-user-event-p event) (stringp (car-safe                           (event-object event))))
  289.            (throw 'popup-done (event-object event)))
  290.           ((and (misc-user-event-p event)
  291.             (or (eq (event-object event) 'abort)
  292.             (eq (event-object event) 'menu-no-selection-hook)))
  293.            nil)
  294.           ((not (popup-menu-up-p))
  295.            (throw 'popup-done nil))
  296.           ((button-release-event-p event);; don't beep twice
  297.            nil)
  298.           (t
  299.            (beep)
  300.            (message "please make a choice from the menu.")))))))
  301.  
  302. ;;; Categories:
  303. ;;
  304. ;; XEmacs use inheritable extents for the same purpose as Emacs uses
  305. ;; the category text property.
  306.  
  307. (if (string-match "XEmacs" emacs-version)
  308.     (progn 
  309.       ;; XEmacs categories.
  310.       (defun custom-category-create (name)
  311.     (set name (make-extent nil nil))
  312.     "Create a text property category named NAME.")
  313.  
  314.       (defun custom-category-put (name property value)
  315.     "In CATEGORY set PROPERTY to VALUE."
  316.     (set-extent-property (symbol-value name) property value))
  317.       
  318.       (defun custom-category-get (name property)
  319.     "In CATEGORY get PROPERTY."
  320.     (extent-property (symbol-value name) property))
  321.       
  322.       (defun custom-category-set (from to category)
  323.     "Make text between FROM and TWO have category CATEGORY."
  324.     (let ((extent (make-extent from to)))
  325.       (set-extent-parent extent (symbol-value category)))))
  326.       
  327.   ;; Emacs categories.
  328.   (defun custom-category-create (name)
  329.     "Create a text property category named NAME."
  330.     (set name name))
  331.  
  332.   (defun custom-category-put (name property value)
  333.     "In CATEGORY set PROPERTY to VALUE."
  334.     (put name property value))
  335.  
  336.   (defun custom-category-get (name property)
  337.     "In CATEGORY get PROPERTY."
  338.     (get name property))
  339.  
  340.   (defun custom-category-set (from to category)
  341.     "Make text between FROM and TWO have category CATEGORY."
  342.     (custom-put-text-property from to 'category category)))
  343.  
  344. ;;; External Data:
  345. ;; 
  346. ;; The following functions and variables defines the interface for
  347. ;; connecting a CUSTOM with an external entity, by default an emacs
  348. ;; lisp variable.
  349.  
  350. (defvar custom-external 'default-value
  351.   "Function returning the external value of NAME.")
  352.  
  353. (defvar custom-external-set 'set-default
  354.   "Function setting the external value of NAME to VALUE.")
  355.  
  356. (defun custom-external (name)
  357.   "Get the external value associated with NAME."
  358.   (funcall custom-external name))
  359.  
  360. (defun custom-external-set (name value)
  361.   "Set the external value associated with NAME to VALUE."
  362.   (funcall custom-external-set name value))
  363.  
  364. (defvar custom-name-fields nil
  365.   "Alist of custom names and their associated editing field.")
  366. (make-variable-buffer-local 'custom-name-fields)
  367.  
  368. (defun custom-name-enter (name field)
  369.   "Associate NAME with FIELD."
  370.   (if (null name)
  371.       ()
  372.     (custom-assert 'field)
  373.     (setq custom-name-fields (cons (cons name field) custom-name-fields))))
  374.  
  375. (defun custom-name-field (name)
  376.   "The editing field associated with NAME."
  377.   (cdr (assq name custom-name-fields)))
  378.  
  379. (defun custom-name-value (name)
  380.   "The value currently displayed for NAME in the customization buffer."
  381.   (let* ((field (custom-name-field name))
  382.      (custom (custom-field-custom field)))
  383.     (custom-field-parse field)
  384.     (funcall (custom-property custom 'export) custom
  385.          (car (custom-field-extract custom field)))))
  386.  
  387. (defvar custom-save 'custom-save
  388.   "Function that will save current customization buffer.")
  389.  
  390. ;;; Custom Functions:
  391. ;;
  392. ;; The following functions are part of the public interface to the
  393. ;; CUSTOM datastructure.  Each CUSTOM describes a group of variables,
  394. ;; a single variable, or a component of a structured variable.  The
  395. ;; CUSTOM instances are part of two hierarchies, the first is the
  396. ;; `part-of' hierarchy in which each CUSTOM is a component of another
  397. ;; CUSTOM, except for the top level CUSTOM which is contained in
  398. ;; `custom-data'.  The second hierarchy is a `is-a' type hierarchy
  399. ;; where each CUSTOM is a leaf in the hierarchy defined by the `type'
  400. ;; property and `custom-type-properties'.
  401.  
  402. (defvar custom-file "~/.custom.el"
  403.   "Name of file with customization information.")
  404.  
  405. (defconst custom-data
  406.   '((tag . "Emacs")
  407.     (doc . "The extensible self-documenting text editor.")
  408.     (type . group)
  409.     (data "\n"
  410.       ((header . nil)
  411.        (compact . t)
  412.        (type . group)
  413.        (doc . "\
  414. Press [Save] to save any changes permanently after you are done editing.  
  415. You can load customization information from other files by editing the
  416. `File' field and pressing the [Load] button.  When you press [Save] the
  417. customization information of all files you have loaded, plus any
  418. changes you might have made manually, will be stored in the file 
  419. specified by the `File' field.")
  420.        (data ((tag . "Load")
  421.           (type . button)
  422.           (query . custom-load))
  423.          ((tag . "Save")
  424.           (type . button)
  425.           (query . custom-save))
  426.          ((name . custom-file)
  427.           (default . "~/.custom.el")
  428.           (doc . "Name of file with customization information.\n")
  429.           (tag . "File")
  430.           (type . file))))))
  431.   "The global customization information.  
  432. A custom association list.")
  433.  
  434. (defun custom-declare (path custom)
  435.   "Declare variables for customization.  
  436. PATH is a list of tags leading to the place in the customization
  437. hierarchy the new entry should be added.  CUSTOM is the entry to add."
  438.   (custom-initialize custom)
  439.   (let ((current (custom-travel-path custom-data path)))
  440.     (or (member custom (custom-data current))
  441.     (nconc (custom-data current) (list custom)))))
  442.  
  443. (put 'custom-declare 'lisp-indent-hook 1)
  444.  
  445. (defconst custom-type-properties
  446.   '((repeat (type . default)
  447.         ;; See `custom-match'.
  448.         (import . custom-repeat-import)
  449.         (eval . custom-repeat-eval)
  450.         (quote . custom-repeat-quote)
  451.         (accept . custom-repeat-accept)
  452.         (extract . custom-repeat-extract)
  453.         (validate . custom-repeat-validate)
  454.         (insert . custom-repeat-insert)
  455.         (match . custom-repeat-match)
  456.         (query . custom-repeat-query)
  457.         (prefix . "")
  458.         (del-tag . "[DEL]")
  459.         (add-tag . "[INS]"))
  460.     (pair (type . group)
  461.       ;; A cons-cell.
  462.       (accept . custom-pair-accept)
  463.       (eval . custom-pair-eval)
  464.       (import . custom-pair-import)
  465.       (quote . custom-pair-quote)
  466.       (valid . (lambda (c d) (consp d)))
  467.       (extract . custom-pair-extract))
  468.     (list (type . group)
  469.       ;; A lisp list.
  470.       (quote . custom-list-quote)
  471.       (valid . (lambda (c d)
  472.              (listp d)))
  473.       (extract . custom-list-extract))
  474.     (group (type . default)
  475.        ;; See `custom-match'.
  476.        (face-tag . nil)
  477.        (eval . custom-group-eval)
  478.        (import . custom-group-import)
  479.        (initialize . custom-group-initialize)
  480.        (apply . custom-group-apply)
  481.        (reset . custom-group-reset)
  482.        (factory-reset . custom-group-factory-reset)
  483.        (extract . nil)
  484.        (validate . custom-group-validate)
  485.        (query . custom-toggle-hide)
  486.        (accept . custom-group-accept)
  487.        (insert . custom-group-insert)
  488.        (find . custom-group-find))
  489.     (toggle (type . choice)
  490.         ;; Booleans.
  491.         (data ((type . const)
  492.            (tag . "On ")
  493.            (default . t))
  494.           ((type . const)
  495.            (tag . "Off")
  496.            (default . nil))))
  497.     (triggle (type . choice)
  498.          ;; On/Off/Default.
  499.          (data ((type . const)
  500.             (tag . "On ")
  501.             (default . t))
  502.            ((type . const)
  503.             (tag . "Off")
  504.             (default . nil))
  505.            ((type . const)
  506.             (tag . "Def")
  507.             (default . custom:asis))))
  508.     (choice (type . default)
  509.         ;; See `custom-match'.
  510.         (query . custom-choice-query)
  511.         (accept . custom-choice-accept)
  512.         (extract . custom-choice-extract)
  513.         (validate . custom-choice-validate)
  514.         (insert . custom-choice-insert)
  515.         (none (tag . "Unknown")
  516.           (default . __uninitialized__)
  517.           (type . const)))
  518.     (const (type . default)
  519.        ;; A `const' only matches a single lisp value.
  520.        (extract . (lambda (c f) (list (custom-default c))))
  521.        (validate . (lambda (c f) nil))
  522.        (valid . custom-const-valid)
  523.        (update . custom-const-update)
  524.        (insert . custom-const-insert))
  525.     (face-doc (type . doc)
  526.           ;; A variable containing a face.
  527.           (doc . "\
  528. You can customize the look of Emacs by deciding which faces should be
  529. used when.  If you push one of the face buttons below, you will be
  530. given a choice between a number of standard faces.  The name of the
  531. selected face is shown right after the face button, and it is
  532. displayed its own face so you can see how it looks.  If you know of
  533. another standard face not listed and want to use it, you can select
  534. `Other' and write the name in the editing field.
  535.  
  536. If none of the standard faces suits you, you can select `Customize' to
  537. create your own face.  This will make six fields appear under the face
  538. button.  The `Fg' and `Bg' fields are the foreground and background
  539. colors for the face, respectively.  You should type the name of the
  540. color in the field.  You can use any X11 color name.  A list of X11
  541. color names may be available in the file `/usr/lib/X11/rgb.txt' on
  542. your system.  The special color name `default' means that the face
  543. will not change the color of the text.  The `Stipple' field is weird,
  544. so just ignore it.  The three remaining fields are toggles, which will
  545. make the text `bold', `italic', or `underline' respectively.  For some
  546. fonts `bold' or `italic' will not make any visible change."))
  547.     (face (type . choice)
  548.       (eval . custom-face-eval)
  549.       (import . custom-face-import)
  550.       (data ((tag . "None")
  551.          (default . nil)
  552.          (type . const))
  553.         ((tag . "Default")
  554.          (default . default)
  555.          (face . custom-const-face)
  556.          (type . const))
  557.         ((tag . "Bold")
  558.          (default . bold)
  559.          (face . custom-const-face)
  560.          (type . const))
  561.         ((tag . "Bold-italic")
  562.          (default . bold-italic)
  563.          (face . custom-const-face)
  564.          (type . const))
  565.         ((tag . "Italic")
  566.          (default . italic)
  567.          (face . custom-const-face)
  568.          (type . const))
  569.         ((tag . "Underline")
  570.          (default . underline)
  571.          (face . custom-const-face)
  572.          (type . const))
  573.         ((tag . "Highlight")
  574.          (default . highlight)
  575.          (face . custom-const-face)
  576.          (type . const))
  577.         ((tag . "Modeline")
  578.          (default . modeline)
  579.          (face . custom-const-face)
  580.          (type . const))
  581.         ((tag . "Region")
  582.          (default . region)
  583.          (face . custom-const-face)
  584.          (type . const))
  585.         ((tag . "Secondary Selection")
  586.          (default . secondary-selection)
  587.          (face . custom-const-face)
  588.          (type . const))
  589.         ((tag . "Customized")
  590.          (compact . t)
  591.          (face-tag . custom-face-hack)
  592.          (eval . custom-face-eval)
  593.          (data ((hidden . t)
  594.             (tag . "")
  595.             (doc . "\
  596. Select the properties you want this face to have.")
  597.             (default . custom-face-lookup)
  598.             (type . const))
  599.                "\n"
  600.                ((tag . "Fg")
  601.             (hidden . t)
  602.             (default . "default")
  603.             (width . 20)
  604.             (type . string))
  605.                ((tag . "Bg")
  606.             (default . "default")
  607.             (width . 20)
  608.             (type . string))
  609.                ((tag . "Stipple")
  610.             (default . "default")
  611.             (width . 20)
  612.             (type . string))
  613.                "\n"
  614.                ((tag . "Bold")
  615.             (default . custom:asis)
  616.             (type . triggle))
  617.                "              "
  618.                ((tag . "Italic")
  619.             (default . custom:asis)
  620.             (type . triggle))
  621.                "             "
  622.                ((tag . "Underline")
  623.             (hidden . t)
  624.             (default . custom:asis)
  625.             (type . triggle)))
  626.          (default . (custom-face-lookup "default" "default" "default"
  627.                         nil nil nil))
  628.          (type . list))
  629.         ((prompt . "Other")
  630.          (face . custom-field-value)
  631.          (default . __uninitialized__)
  632.          (type . symbol))))
  633.     (file (type . string)
  634.       ;; A string containing a file or directory name.
  635.       (directory . nil)
  636.       (default-file . nil)
  637.       (query . custom-file-query))
  638.     (sexp (type . default)
  639.       ;; Any lisp expression.
  640.       (width . 40)
  641.       (default . (__uninitialized__ . "Uninitialized"))
  642.       (read . custom-sexp-read)
  643.       (write . custom-sexp-write))
  644.     (symbol (type . sexp)
  645.         ;; A lisp symbol.
  646.         (width . 40)
  647.         (valid . (lambda (c d) (symbolp d))))
  648.     (integer (type . sexp)
  649.          ;; A lisp integer.
  650.          (width . 10)
  651.          (valid . (lambda (c d) (integerp d))))
  652.     (string (type . default)
  653.         ;; A lisp string.
  654.         (width . 40) 
  655.         (valid . (lambda (c d) (stringp d)))
  656.         (read . custom-string-read)
  657.         (write . custom-string-write))
  658.     (button (type . default)
  659.         ;; Push me.
  660.         (accept . ignore)
  661.         (extract . nil)
  662.         (validate . ignore)
  663.         (insert . custom-button-insert))
  664.     (doc (type . default)
  665.      ;; A documentation only entry with no value.
  666.      (header . nil)
  667.      (reset . ignore)
  668.      (extract . nil)
  669.      (validate . ignore)
  670.      (insert . custom-documentation-insert))
  671.     (default (width . 20)
  672.              (valid . (lambda (c v) t))
  673.          (insert . custom-default-insert)
  674.          (update . custom-default-update)
  675.          (query . custom-default-query)
  676.          (tag . nil)
  677.          (prompt . nil)
  678.          (doc . nil)
  679.          (header . t)
  680.          (padding . ? )
  681.          (quote . custom-default-quote)
  682.          (eval . (lambda (c v) nil))
  683.          (export . custom-default-export)
  684.          (import . (lambda (c v) (list v)))
  685.          (synchronize . ignore)
  686.          (initialize . custom-default-initialize)
  687.          (extract . custom-default-extract)
  688.          (validate . custom-default-validate)
  689.          (apply . custom-default-apply)
  690.          (reset . custom-default-reset)
  691.          (factory-reset . custom-default-factory-reset)
  692.          (accept . custom-default-accept)
  693.          (match . custom-default-match)
  694.          (name . nil)
  695.          (compact . nil)
  696.          (hidden . nil)
  697.          (face . custom-default-face)
  698.          (data . nil)
  699.          (calculate . nil)
  700.          (default . __uninitialized__)))
  701.   "Alist of default properties for type symbols.
  702. The format is `((SYMBOL (PROPERTY . VALUE)... )... )'.")
  703.  
  704. (defconst custom-local-type-properties nil
  705.   "Local type properties.
  706. Entries in this list take precedence over `custom-type-properties'.")
  707.  
  708. (make-variable-buffer-local 'custom-local-type-properties)
  709.  
  710. (defconst custom-nil '__uninitialized__
  711.   "Special value representing an uninitialized field.")
  712.  
  713. (defconst custom-invalid '__invalid__
  714.   "Special value representing an invalid field.")
  715.  
  716. (defconst custom:asis 'custom:asis)
  717. ;; Bad, ugly, and horrible kludge.
  718.  
  719. (defun custom-property (custom property)
  720.   "Extract from CUSTOM property PROPERTY."
  721.   (let ((entry (assq property custom)))
  722.     (while (null entry)
  723.       ;; Look in superclass.
  724.       (let ((type (custom-type custom)))
  725.     (setq custom (cdr (or (assq type custom-local-type-properties)
  726.                   (assq type custom-type-properties)))
  727.           entry (assq property custom))
  728.     (custom-assert 'custom)))
  729.     (cdr entry)))
  730.  
  731. (defun custom-super (custom property)
  732.   "Extract from CUSTOM property PROPERTY.  Start with CUSTOM's superclass."
  733.   (let ((entry nil))
  734.     (while (null entry)
  735.       ;; Look in superclass.
  736.       (let ((type (custom-type custom)))
  737.     (setq custom (cdr (or (assq type custom-local-type-properties)
  738.                   (assq type custom-type-properties)))
  739.           entry (assq property custom))
  740.     (custom-assert 'custom)))
  741.     (cdr entry)))
  742.  
  743. (defun custom-property-set (custom property value)
  744.   "Set CUSTOM PROPERTY to VALUE by side effect.
  745. CUSTOM must have at least one property already."
  746.   (let ((entry (assq property custom)))
  747.     (if entry
  748.     (setcdr entry value)
  749.       (setcdr custom (cons (cons property value) (cdr custom))))))
  750.  
  751. (defun custom-type (custom)
  752.   "Extract `type' from CUSTOM."
  753.   (cdr (assq 'type custom)))
  754.  
  755. (defun custom-name (custom)
  756.   "Extract `name' from CUSTOM."
  757.   (custom-property custom 'name))
  758.  
  759. (defun custom-tag (custom)
  760.   "Extract `tag' from CUSTOM."
  761.   (custom-property custom 'tag))
  762.  
  763. (defun custom-face-tag (custom)
  764.   "Extract `face-tag' from CUSTOM."
  765.   (custom-property custom 'face-tag))
  766.  
  767. (defun custom-prompt (custom)
  768.   "Extract `prompt' from CUSTOM.  
  769. If none exist, default to `tag' or, failing that, `type'."
  770.   (or (custom-property custom 'prompt)
  771.       (custom-property custom 'tag)
  772.       (capitalize (symbol-name (custom-type custom)))))
  773.  
  774. (defun custom-default (custom)
  775.   "Extract `default' from CUSTOM."
  776.   (let ((value (custom-property custom 'calculate)))
  777.     (if value
  778.     (eval value)
  779.       (custom-property custom 'default))))
  780.            
  781. (defun custom-data (custom)
  782.   "Extract the `data' from CUSTOM."
  783.   (custom-property custom 'data))
  784.  
  785. (defun custom-documentation (custom)
  786.   "Extract `doc' from CUSTOM."
  787.   (custom-property custom 'doc))
  788.  
  789. (defun custom-width (custom)
  790.   "Extract `width' from CUSTOM."
  791.   (custom-property custom 'width))
  792.  
  793. (defun custom-compact (custom)
  794.   "Extract `compact' from CUSTOM."
  795.   (custom-property custom 'compact))
  796.  
  797. (defun custom-padding (custom)
  798.   "Extract `padding' from CUSTOM."
  799.   (custom-property custom 'padding))
  800.  
  801. (defun custom-valid (custom value)
  802.   "Non-nil if CUSTOM may validly be set to VALUE."
  803.   (and (not (and (listp value) (eq custom-invalid (car value))))
  804.        (funcall (custom-property custom 'valid) custom value)))
  805.  
  806. (defun custom-import (custom value)
  807.   "Import CUSTOM VALUE from external variable.
  808.  
  809. This function change VALUE into a form that makes it easier to edit 
  810. internally.  What the internal form is exactly depends on CUSTOM.  
  811. The internal form is returned."
  812.   (if (eq custom-nil value)
  813.       (list custom-nil)
  814.     (funcall (custom-property custom 'import) custom value)))
  815.  
  816. (defun custom-eval (custom value)
  817.   "Return non-nil if CUSTOM's VALUE needs to be evaluated."
  818.   (funcall (custom-property custom 'eval) custom value))
  819.  
  820. (defun custom-quote (custom value)
  821.   "Quote CUSTOM's VALUE if necessary."
  822.   (funcall (custom-property custom 'quote) custom value))
  823.  
  824. (defun custom-write (custom value)
  825.   "Convert CUSTOM VALUE to a string."
  826.   (cond ((eq value custom-nil) 
  827.      "")
  828.     ((and (listp value) (eq (car value) custom-invalid))
  829.      (cdr value))
  830.     (t
  831.      (funcall (custom-property custom 'write) custom value))))
  832.  
  833. (defun custom-read (custom string)
  834.   "Convert CUSTOM field content STRING into lisp."
  835.   (condition-case nil
  836.       (funcall (custom-property custom 'read) custom string)
  837.     (error (cons custom-invalid string))))
  838.  
  839. (defun custom-match (custom values)
  840.   "Match CUSTOM with a list of VALUES.
  841.  
  842. Return a cons-cell where the car is the sublist of VALUES matching CUSTOM,
  843. and the cdr is the remaining VALUES.
  844.  
  845. A CUSTOM is actually a regular expression over the alphabet of lisp
  846. types.  Most CUSTOM types are just doing a literal match, e.g. the
  847. `symbol' type matches any lisp symbol.  The exceptions are:
  848.  
  849. group:    which corresponds to a `(' and `)' group in a regular expression.
  850. choice:   which corresponds to a group of `|' in a regular expression.
  851. repeat:   which corresponds to a `*' in a regular expression.
  852. optional: which corresponds to a `?', and isn't implemented yet."
  853.   (if (memq values (list custom-nil nil))
  854.       ;; Nothing matches the uninitialized or empty list.
  855.       (cons custom-nil nil)
  856.     (funcall (custom-property custom 'match) custom values)))
  857.  
  858. (defun custom-initialize (custom)
  859.   "Initialize `doc' and `default' attributes of CUSTOM."
  860.   (funcall (custom-property custom 'initialize) custom))
  861.  
  862. (defun custom-find (custom tag)
  863.   "Find child in CUSTOM with `tag' TAG."
  864.   (funcall (custom-property custom 'find) custom tag))
  865.  
  866. (defun custom-travel-path (custom path)
  867.   "Find decedent of CUSTOM by looking through PATH."
  868.   (if (null path)
  869.       custom
  870.     (custom-travel-path (custom-find custom (car path)) (cdr path))))
  871.  
  872. (defun custom-field-extract (custom field)
  873.   "Extract CUSTOM's value in FIELD."
  874.   (if (stringp custom)
  875.       nil
  876.     (funcall (custom-property (custom-field-custom field) 'extract)
  877.          custom field)))
  878.  
  879. (defun custom-field-validate (custom field)
  880.   "Validate CUSTOM's value in FIELD.
  881. Return nil if valid, otherwise return a cons-cell where the car is the
  882. position of the error, and the cdr is a text describing the error."
  883.   (if (stringp custom)
  884.       nil
  885.     (funcall (custom-property custom 'validate) custom field)))
  886.  
  887. ;;; Field Functions:
  888. ;;
  889. ;; This section defines the public functions for manipulating the
  890. ;; FIELD datatype.  The FIELD instance hold information about a
  891. ;; specific editing field in the customization buffer.
  892. ;;
  893. ;; Each FIELD can be seen as an instantiation of a CUSTOM.
  894.  
  895. (defvar custom-field-last nil)
  896. ;; Last field containing point.
  897. (make-variable-buffer-local 'custom-field-last)
  898.  
  899. (defvar custom-modified-list nil)
  900. ;; List of modified fields.
  901. (make-variable-buffer-local 'custom-modified-list)
  902.  
  903. (defun custom-field-create (custom value)
  904.   "Create a field structure of type CUSTOM containing VALUE.
  905.  
  906. A field structure is an array [ CUSTOM VALUE ORIGINAL START END ], where
  907. CUSTOM defines the type of the field, 
  908. VALUE is the current value of the field,
  909. ORIGINAL is the original value when created, and
  910. START and END are markers to the start and end of the field."
  911.   (vector custom value custom-nil nil nil))
  912.  
  913. (defun custom-field-custom (field)
  914.   "Return the `custom' attribute of FIELD."
  915.   (aref field 0))
  916.   
  917. (defun custom-field-value (field)
  918.   "Return the `value' attribute of FIELD."
  919.   (aref field 1))
  920.  
  921. (defun custom-field-original (field)
  922.   "Return the `original' attribute of FIELD."
  923.   (aref field 2))
  924.  
  925. (defun custom-field-start (field)
  926.   "Return the `start' attribute of FIELD."
  927.   (aref field 3))
  928.  
  929. (defun custom-field-end (field)
  930.   "Return the `end' attribute of FIELD."
  931.   (aref field 4))
  932.   
  933. (defun custom-field-value-set (field value)
  934.   "Set the `value' attribute of FIELD to VALUE."
  935.   (aset field 1 value))
  936.  
  937. (defun custom-field-original-set (field original)
  938.   "Set the `original' attribute of FIELD to ORIGINAL."
  939.   (aset field 2 original))
  940.  
  941. (defun custom-field-move (field start end)
  942.   "Set the `start'and `end' attributes of FIELD to START and END."
  943.   (set-marker (or (aref field 3) (aset field 3 (make-marker))) start)
  944.   (set-marker (or (aref field 4) (aset field 4 (make-marker))) end))
  945.  
  946. (defun custom-field-query (field)
  947.   "Query user for content of current field."
  948.   (funcall (custom-property (custom-field-custom field) 'query) field))
  949.  
  950. (defun custom-field-accept (field value &optional original)
  951.   "Store a new value into field FIELD, taking it from VALUE.
  952. If optional ORIGINAL is non-nil, consider VALUE for the original value."
  953.   (let ((inhibit-point-motion-hooks t))
  954.     (funcall (custom-property (custom-field-custom field) 'accept) 
  955.          field value original)))
  956.  
  957. (defun custom-field-face (field)
  958.   "The face used for highlighting FIELD."
  959.   (let ((custom (custom-field-custom field)))
  960.     (if (stringp custom)
  961.     nil
  962.       (let ((face (funcall (custom-property custom 'face) field)))
  963.     (if (custom-facep face) face nil)))))
  964.  
  965. (defun custom-field-update (field)
  966.   "Update the screen appearance of FIELD to correspond with the field's value."
  967.   (let ((custom (custom-field-custom field)))
  968.     (if (stringp custom)
  969.     nil
  970.       (funcall (custom-property custom 'update) field))))
  971.  
  972. ;;; Types:
  973. ;;
  974. ;; The following functions defines type specific actions.
  975.  
  976. (defun custom-repeat-eval (custom value)
  977.   "Non-nil if CUSTOM's VALUE needs to be evaluated."
  978.   (if (eq value custom-nil)
  979.       nil
  980.     (let ((child (custom-data custom))
  981.       (found nil))
  982.       (mapcar (lambda (v) (if (custom-eval child v) (setq found t)))
  983.           value))))
  984.  
  985. (defun custom-repeat-quote (custom value)
  986.   "A list of CUSTOM's VALUEs quoted."
  987.   (let ((child (custom-data custom)))
  988.     (apply 'append (mapcar (lambda (v) (custom-quote child v))
  989.                value))))
  990.  
  991.   
  992. (defun custom-repeat-import (custom value)
  993.   "Modify CUSTOM's VALUE to match internal expectations."
  994.   (let ((child (custom-data custom)))
  995.     (apply 'append (mapcar (lambda (v) (custom-import child v))
  996.                value))))
  997.  
  998. (defun custom-repeat-accept (field value &optional original)
  999.   "Store a new value into field FIELD, taking it from VALUE."
  1000.   (let ((values (copy-sequence (custom-field-value field)))
  1001.     (all (custom-field-value field))
  1002.     (start (custom-field-start field))
  1003.     current new)
  1004.     (if original 
  1005.     (custom-field-original-set field value))
  1006.     (while (consp value)
  1007.       (setq new (car value)
  1008.         value (cdr value))
  1009.       (if values
  1010.       ;; Change existing field.
  1011.       (setq current (car values)
  1012.         values (cdr values))
  1013.     ;; Insert new field if series has grown.
  1014.     (goto-char start)
  1015.     (setq current (custom-repeat-insert-entry field))
  1016.     (setq all (custom-insert-before all nil current))
  1017.     (custom-field-value-set field all))
  1018.       (custom-field-accept current new original))
  1019.     (while (consp values)
  1020.       ;; Delete old field if series has scrunk.
  1021.       (setq current (car values)
  1022.         values (cdr values))
  1023.       (let ((pos (custom-field-start current))
  1024.         data)
  1025.     (while (not data)
  1026.       (setq pos (previous-single-property-change pos 'custom-data))
  1027.       (custom-assert 'pos)
  1028.       (setq data (get-text-property pos 'custom-data))
  1029.       (or (and (arrayp data)
  1030.            (> (length data) 1)
  1031.            (eq current (aref data 1)))
  1032.           (setq data nil)))
  1033.     (custom-repeat-delete data)))))
  1034.  
  1035. (defun custom-repeat-insert (custom level)
  1036.   "Insert field for CUSTOM at nesting LEVEL in customization buffer."
  1037.   (let* ((field (custom-field-create custom nil))
  1038.      (add-tag (custom-property custom 'add-tag))
  1039.      (start (make-marker))
  1040.      (data (vector field nil start nil)))
  1041.     (custom-text-insert "\n")
  1042.     (let ((pos (point)))
  1043.       (custom-text-insert (custom-property custom 'prefix))
  1044.       (custom-tag-insert add-tag 'custom-repeat-add data)
  1045.       (set-marker start pos))
  1046.     (custom-field-move field start (point))
  1047.     (custom-documentation-insert custom)
  1048.     field))
  1049.  
  1050. (defun custom-repeat-insert-entry (repeat)
  1051.   "Insert entry at point in the REPEAT field."
  1052.   (let* ((inhibit-point-motion-hooks t)
  1053.      (inhibit-read-only t)
  1054.      (before-change-functions nil)
  1055.      (after-change-functions nil)
  1056.      (custom (custom-field-custom repeat))
  1057.      (add-tag (custom-property custom 'add-tag))
  1058.      (del-tag (custom-property custom 'del-tag))
  1059.      (start (make-marker))
  1060.      (end (make-marker))
  1061.      (data (vector repeat nil start end))
  1062.      field)
  1063.     (custom-extent-start-open)
  1064.     (insert-before-markers "\n")
  1065.     (backward-char 1)
  1066.     (set-marker start (point))
  1067.     (custom-text-insert " ")
  1068.     (aset data 1 (setq field (custom-insert (custom-data custom) nil)))
  1069.     (custom-text-insert " ")
  1070.     (set-marker end (point))
  1071.     (goto-char start)
  1072.     (custom-text-insert (custom-property custom 'prefix))
  1073.     (custom-tag-insert add-tag 'custom-repeat-add data)
  1074.     (custom-text-insert " ")
  1075.     (custom-tag-insert del-tag 'custom-repeat-delete data)
  1076.     (forward-char 1)
  1077.     field))
  1078.  
  1079. (defun custom-repeat-add (data)
  1080.   "Add list entry."
  1081.   (let ((parent (aref data 0))
  1082.     (field (aref data 1))
  1083.     (at (aref data 2))
  1084.     new)
  1085.     (goto-char at)
  1086.     (setq new (custom-repeat-insert-entry parent))
  1087.     (custom-field-value-set parent
  1088.                 (custom-insert-before (custom-field-value parent)
  1089.                           field new))))
  1090.  
  1091. (defun custom-repeat-delete (data)
  1092.   "Delete list entry."
  1093.   (let ((inhibit-point-motion-hooks t)
  1094.     (inhibit-read-only t)
  1095.     (before-change-functions nil)
  1096.     (after-change-functions nil)
  1097.     (parent (aref data 0))
  1098.     (field (aref data 1)))
  1099.     (delete-region (aref data 2) (1+ (aref data 3)))
  1100.     (custom-field-untouch (aref data 1))
  1101.     (custom-field-value-set parent 
  1102.                 (delq field (custom-field-value parent)))))
  1103.  
  1104. (defun custom-repeat-match (custom values)
  1105.   "Match CUSTOM with VALUES."
  1106.   (let* ((child (custom-data custom))
  1107.      (match (custom-match child values))
  1108.      matches)
  1109.     (while (not (eq (car match) custom-nil))
  1110.       (setq matches (cons (car match) matches)
  1111.         values (cdr match)
  1112.         match (custom-match child values)))
  1113.     (cons (nreverse matches) values)))
  1114.  
  1115. (defun custom-repeat-extract (custom field)
  1116.   "Extract list of children's values."
  1117.   (let ((values (custom-field-value field))
  1118.     (data (custom-data custom))
  1119.     result)
  1120.     (if (eq values custom-nil)
  1121.     ()
  1122.       (while values
  1123.     (setq result (append result (custom-field-extract data (car values)))
  1124.           values (cdr values))))
  1125.     result))
  1126.  
  1127. (defun custom-repeat-validate (custom field)
  1128.   "Validate children."
  1129.   (let ((values (custom-field-value field))
  1130.     (data (custom-data custom))
  1131.     result)
  1132.     (if (eq values custom-nil)
  1133.     (setq result (cons (custom-field-start field) "Uninitialized list")))
  1134.     (while (and values (not result))
  1135.       (setq result (custom-field-validate data (car values))
  1136.         values (cdr values)))
  1137.     result))
  1138.  
  1139. (defun custom-pair-accept (field value &optional original)
  1140.   "Store a new value into field FIELD, taking it from VALUE."
  1141.   (custom-group-accept field (list (car value) (cdr value)) original))
  1142.  
  1143. (defun custom-pair-eval (custom value)
  1144.   "Non-nil if CUSTOM's VALUE needs to be evaluated."
  1145.   (custom-group-eval custom (list (car value) (cdr value))))
  1146.  
  1147. (defun custom-pair-import (custom value)
  1148.   "Modify CUSTOM's VALUE to match internal expectations."
  1149.   (let ((result (car (custom-group-import custom 
  1150.                       (list (car value) (cdr value))))))
  1151.     (custom-assert '(eq (length result) 2))
  1152.     (list (cons (nth 0 result) (nth 1 result)))))
  1153.  
  1154. (defun custom-pair-quote (custom value)
  1155.   "Quote CUSTOM's VALUE if necessary."
  1156.   (if (custom-eval custom value)
  1157.       (let ((v (car (custom-group-quote custom 
  1158.                     (list (car value) (cdr value))))))
  1159.     (list (list 'cons (nth 0 v) (nth 1 v))))
  1160.     (custom-default-quote custom value)))
  1161.  
  1162. (defun custom-pair-extract (custom field)
  1163.   "Extract cons of children's values."
  1164.   (let ((values (custom-field-value field))
  1165.     (data (custom-data custom))
  1166.     result)
  1167.     (custom-assert '(eq (length values) (length data)))
  1168.     (while values
  1169.       (setq result (append result
  1170.                (custom-field-extract (car data) (car values)))
  1171.         data (cdr data)
  1172.         values (cdr values)))
  1173.     (custom-assert '(null data))
  1174.     (list (cons (nth 0 result) (nth 1 result)))))
  1175.  
  1176. (defun custom-list-quote (custom value)
  1177.   "Quote CUSTOM's VALUE if necessary."
  1178.   (if (custom-eval custom value)
  1179.       (let ((v (car (custom-group-quote custom value))))
  1180.     (list (cons 'list v)))
  1181.     (custom-default-quote custom value)))
  1182.  
  1183. (defun custom-list-extract (custom field)
  1184.   "Extract list of children's values."
  1185.   (let ((values (custom-field-value field))
  1186.     (data (custom-data custom))
  1187.     result)
  1188.     (custom-assert '(eq (length values) (length data)))
  1189.     (while values
  1190.       (setq result (append result
  1191.                (custom-field-extract (car data) (car values)))
  1192.         data (cdr data)
  1193.         values (cdr values)))
  1194.     (custom-assert '(null data))
  1195.     (list result)))
  1196.  
  1197. (defun custom-group-validate (custom field)
  1198.   "Validate children."
  1199.   (let ((values (custom-field-value field))
  1200.     (data (custom-data custom))
  1201.     result)
  1202.     (if (eq values custom-nil)
  1203.     (setq result (cons (custom-field-start field) "Uninitialized list"))
  1204.       (custom-assert '(eq (length values) (length data))))
  1205.     (while (and values (not result))
  1206.       (setq result (custom-field-validate (car data) (car values))
  1207.         data (cdr data)
  1208.         values (cdr values)))
  1209.     result))
  1210.  
  1211. (defun custom-group-eval (custom value)
  1212.   "Non-nil if CUSTOM's VALUE needs to be evaluated."
  1213.   (let ((found nil))
  1214.     (mapcar (lambda (c)
  1215.           (or (stringp c)
  1216.           (let ((match (custom-match c value)))
  1217.             (if (custom-eval c (car match))
  1218.             (setq found t))
  1219.             (setq value (cdr match)))))
  1220.         (custom-data custom))
  1221.     found))
  1222.  
  1223. (defun custom-group-quote (custom value)
  1224.   "A list of CUSTOM's VALUE members, quoted."
  1225.   (list (apply 'append 
  1226.            (mapcar (lambda (c)
  1227.              (if (stringp c)
  1228.                  ()
  1229.                (let ((match (custom-match c value)))
  1230.                  (prog1 (custom-quote c (car match))
  1231.                    (setq value (cdr match))))))
  1232.                (custom-data custom)))))
  1233.  
  1234. (defun custom-group-import (custom value)
  1235.   "Modify CUSTOM's VALUE to match internal expectations."
  1236.   (list (apply 'append 
  1237.            (mapcar (lambda (c)
  1238.              (if (stringp c)
  1239.                  ()
  1240.                (let ((match (custom-match c value)))
  1241.                  (prog1 (custom-import c (car match))
  1242.                    (setq value (cdr match))))))
  1243.                (custom-data custom)))))
  1244.  
  1245. (defun custom-group-initialize (custom)
  1246.   "Initialize `doc' and `default' entries in CUSTOM."
  1247.   (if (custom-name custom)
  1248.       (custom-default-initialize custom)
  1249.     (mapcar 'custom-initialize (custom-data custom))))
  1250.  
  1251. (defun custom-group-apply (field)
  1252.   "Reset `value' in FIELD to `original'."
  1253.   (let ((custom (custom-field-custom field))
  1254.     (values (custom-field-value field)))
  1255.     (if (custom-name custom)
  1256.     (custom-default-apply field)
  1257.       (mapcar 'custom-field-apply values))))
  1258.  
  1259. (defun custom-group-reset (field)
  1260.   "Reset `value' in FIELD to `original'."
  1261.   (let ((custom (custom-field-custom field))
  1262.     (values (custom-field-value field)))
  1263.     (if (custom-name custom)
  1264.     (custom-default-reset field)
  1265.       (mapcar 'custom-field-reset values))))
  1266.  
  1267. (defun custom-group-factory-reset (field)
  1268.   "Reset `value' in FIELD to `default'."
  1269.   (let ((custom (custom-field-custom field))
  1270.     (values (custom-field-value field)))
  1271.     (if (custom-name custom)
  1272.     (custom-default-factory-reset field)
  1273.       (mapcar 'custom-field-factory-reset values))))
  1274.  
  1275. (defun custom-group-find (custom tag)
  1276.   "Find child in CUSTOM with `tag' TAG."
  1277.   (let ((data (custom-data custom))
  1278.     (result nil))
  1279.     (while (not result)
  1280.       (custom-assert 'data)
  1281.       (if (equal (custom-tag (car data)) tag)
  1282.       (setq result (car data))
  1283.     (setq data (cdr data))))))
  1284.  
  1285. (defun custom-group-accept (field value &optional original)
  1286.   "Store a new value into field FIELD, taking it from VALUE."
  1287.   (let* ((values (custom-field-value field))
  1288.      (custom (custom-field-custom field))
  1289.      (from (custom-field-start field))
  1290.      (face-tag (custom-face-tag custom))
  1291.      current)
  1292.     (if face-tag 
  1293.     (custom-put-text-property from (+ from (length (custom-tag custom)))
  1294.                'face (funcall face-tag field value)))
  1295.     (if original 
  1296.     (custom-field-original-set field value))
  1297.     (while values
  1298.       (setq current (car values)
  1299.         values (cdr values))
  1300.       (if current
  1301.       (let* ((custom (custom-field-custom current))
  1302.          (match (custom-match custom value)))
  1303.         (setq value (cdr match))
  1304.         (custom-field-accept current (car match) original))))))
  1305.  
  1306. (defun custom-group-insert (custom level)
  1307.   "Insert field for CUSTOM at nesting LEVEL in customization buffer."
  1308.   (let* ((field (custom-field-create custom nil))
  1309.      fields hidden
  1310.      (from (point))
  1311.      (compact (custom-compact custom))
  1312.      (tag (custom-tag custom))
  1313.      (face-tag (custom-face-tag custom)))
  1314.     (cond (face-tag (custom-text-insert tag))
  1315.       (tag (custom-tag-insert tag field)))
  1316.     (or compact (custom-documentation-insert custom))
  1317.     (or compact (custom-text-insert "\n"))
  1318.     (let ((data (custom-data custom)))
  1319.       (while data
  1320.     (setq fields (cons (custom-insert (car data) (if level (1+ level)))
  1321.                fields))
  1322.     (setq hidden (or (stringp (car data))
  1323.              (custom-property (car data) 'hidden)))
  1324.     (setq data (cdr data))
  1325.     (if data (custom-text-insert (cond (hidden "")
  1326.                        (compact " ")
  1327.                        (t "\n"))))))
  1328.     (if compact (custom-documentation-insert custom))
  1329.     (custom-field-value-set field (nreverse fields))
  1330.     (custom-field-move field from (point))
  1331.     field))
  1332.  
  1333. (defun custom-choice-insert (custom level)
  1334.   "Insert field for CUSTOM at nesting LEVEL in customization buffer."
  1335.   (let* ((field (custom-field-create custom nil))
  1336.      (from (point)))
  1337.     (custom-text-insert "lars er en nisse")
  1338.     (custom-field-move field from (point))
  1339.     (custom-documentation-insert custom)
  1340.     (custom-field-reset field)
  1341.     field))
  1342.  
  1343. (defun custom-choice-accept (field value &optional original)
  1344.   "Store a new value into field FIELD, taking it from VALUE."
  1345.   (let ((custom (custom-field-custom field))
  1346.     (start (custom-field-start field))
  1347.     (end (custom-field-end field))
  1348.     (inhibit-read-only t)
  1349.     (before-change-functions nil)
  1350.     (after-change-functions nil)
  1351.     from)
  1352.     (cond (original 
  1353.        (setq custom-modified-list (delq field custom-modified-list))
  1354.        (custom-field-original-set field value))
  1355.       ((equal value (custom-field-original field))
  1356.        (setq custom-modified-list (delq field custom-modified-list)))
  1357.       (t
  1358.        (add-to-list 'custom-modified-list field)))
  1359.     (custom-field-untouch (custom-field-value field))
  1360.     (delete-region start end)
  1361.     (goto-char start)
  1362.     (setq from (point))
  1363.     (insert-before-markers " ")
  1364.     (backward-char 1)
  1365.     (custom-category-set (point) (1+ (point)) 'custom-hidden-properties)
  1366.     (custom-tag-insert (custom-tag custom) field)
  1367.     (custom-text-insert ": ")
  1368.     (let ((data (custom-data custom))
  1369.       found begin)
  1370.       (while (and data (not found))
  1371.     (if (not (custom-valid (car data) value))
  1372.         (setq data (cdr data))
  1373.       (setq found (custom-insert (car data) nil))
  1374.       (setq data nil)))
  1375.       (if found 
  1376.       ()
  1377.     (setq begin (point)
  1378.           found (custom-insert (custom-property custom 'none) nil))
  1379.     (custom-add-text-properties 
  1380.      begin (point)
  1381.      (list rear-nonsticky t
  1382.            'face custom-field-uninitialized-face)))
  1383.       (or original
  1384.       (custom-field-original-set found (custom-field-original field)))
  1385.       (custom-field-accept found value original)
  1386.       (custom-field-value-set field found)
  1387.       (custom-field-move field from end))))
  1388.  
  1389. (defun custom-choice-extract (custom field)
  1390.   "Extract child's value."
  1391.   (let ((value (custom-field-value field)))
  1392.     (custom-field-extract (custom-field-custom value) value)))
  1393.  
  1394. (defun custom-choice-validate (custom field)
  1395.   "Validate child's value."
  1396.   (let ((value (custom-field-value field))
  1397.     (custom (custom-field-custom field)))
  1398.     (if (or (eq value custom-nil)
  1399.         (eq (custom-field-custom value) (custom-property custom 'none)))
  1400.     (cons (custom-field-start field) "Make a choice")
  1401.       (custom-field-validate (custom-field-custom value) value))))
  1402.  
  1403. (defun custom-choice-query (field)
  1404.   "Choose a child."
  1405.   (let* ((custom (custom-field-custom field))
  1406.      (old (custom-field-custom (custom-field-value field)))
  1407.      (default (custom-prompt old))
  1408.      (tag (custom-prompt custom))
  1409.      (data (custom-data custom))
  1410.      current alist)
  1411.     (if (eq (length data) 2)
  1412.     (custom-field-accept field (custom-default (if (eq (nth 0 data) old)
  1413.                                (nth 1 data)
  1414.                              (nth 0 data))))
  1415.       (while data
  1416.     (setq current (car data)
  1417.           data (cdr data))
  1418.     (setq alist (cons (cons (custom-prompt current) current) alist)))
  1419.       (let ((answer (cond ((and (fboundp 'button-press-event-p)
  1420.                 (fboundp 'popup-menu)
  1421.                 (button-press-event-p last-input-event))
  1422.                (cdr (assoc (car (custom-x-really-popup-menu 
  1423.                          last-input-event tag 
  1424.                          (reverse alist)))
  1425.                        alist)))
  1426.               ((listp last-input-event)
  1427.                (x-popup-menu last-input-event
  1428.                      (list tag (cons "" (reverse alist)))))
  1429.               (t 
  1430.                (let ((choice (completing-read (concat tag
  1431.                                   " (default "
  1432.                                   default 
  1433.                                   "): ") 
  1434.                               alist nil t)))
  1435.                  (if (or (null choice) (string-equal choice ""))
  1436.                  (setq choice default))
  1437.                  (cdr (assoc choice alist)))))))
  1438.     (if answer
  1439.         (custom-field-accept field (custom-default answer)))))))
  1440.  
  1441. (defun custom-file-query (field)
  1442.   "Prompt for a file name"
  1443.   (let* ((value (custom-field-value field))
  1444.      (custom (custom-field-custom field))
  1445.      (valid (custom-valid custom value))
  1446.      (directory (custom-property custom 'directory))
  1447.      (default (and (not valid)
  1448.                (custom-property custom 'default-file)))
  1449.      (tag (custom-tag custom))
  1450.      (prompt (if default
  1451.              (concat tag " (" default "): ")
  1452.            (concat tag ": "))))
  1453.     (custom-field-accept field 
  1454.              (if (custom-valid custom value)
  1455.                  (read-file-name prompt 
  1456.                          (if (file-name-absolute-p value)
  1457.                          ""
  1458.                            directory)
  1459.                          default nil value)
  1460.                (read-file-name prompt directory default)))))
  1461.  
  1462. (defun custom-face-eval (custom value)
  1463.   "Return non-nil if CUSTOM's VALUE needs to be evaluated."
  1464.   (not (symbolp value)))
  1465.  
  1466. (defun custom-face-import (custom value)
  1467.   "Modify CUSTOM's VALUE to match internal expectations."
  1468.   (let ((name (or (and (facep value) (symbol-name (face-name value)))
  1469.           (symbol-name value))))
  1470.     (list (if (string-match "\
  1471. custom-face-\\(.*\\)-\\(.*\\)-\\(.*\\)-\\(.*\\)-\\(.*\\)-\\(.*\\)"
  1472.                 name)
  1473.           (list 'custom-face-lookup 
  1474.             (match-string 1 name)
  1475.             (match-string 2 name)
  1476.             (match-string 3 name)
  1477.             (intern (match-string 4 name))
  1478.             (intern (match-string 5 name))
  1479.             (intern (match-string 6 name)))
  1480.         value))))
  1481.  
  1482. (defun custom-face-lookup (&optional fg bg stipple bold italic underline)
  1483.   "Lookup or create a face with specified attributes."
  1484.   (let ((name (intern (format "custom-face-%s-%s-%s-%S-%S-%S"
  1485.                   (or fg "default")
  1486.                   (or bg "default")
  1487.                   (or stipple "default")
  1488.                   bold italic underline))))
  1489.     (if (and (custom-facep name)
  1490.          (fboundp 'make-face))
  1491.     ()
  1492.       (copy-face 'default name)
  1493.       (when (and fg
  1494.          (not (string-equal fg "default")))
  1495.     (condition-case ()
  1496.         (set-face-foreground name fg)
  1497.       (error nil)))
  1498.       (when (and bg
  1499.          (not (string-equal bg "default")))
  1500.     (condition-case ()
  1501.         (set-face-background name bg)
  1502.       (error nil)))
  1503.       (when (and stipple
  1504.          (not (string-equal stipple "default"))
  1505.          (not (eq stipple 'custom:asis))
  1506.          (fboundp 'set-face-stipple))
  1507.     (set-face-stipple name stipple))
  1508.       (when (and bold
  1509.          (not (eq bold 'custom:asis)))
  1510.     (condition-case ()
  1511.         (make-face-bold name)
  1512.       (error nil)))
  1513.       (when (and italic
  1514.          (not (eq italic 'custom:asis)))
  1515.     (condition-case ()
  1516.         (make-face-italic name)
  1517.       (error nil)))
  1518.       (when (and underline
  1519.          (not (eq underline 'custom:asis)))
  1520.     (condition-case ()
  1521.         (set-face-underline-p name t)
  1522.       (error nil))))
  1523.     name))
  1524.  
  1525. (defun custom-face-hack (field value)
  1526.   "Face that should be used for highlighting FIELD containing VALUE."
  1527.   (let* ((custom (custom-field-custom field))
  1528.      (form (funcall (custom-property custom 'export) custom value))
  1529.      (face (apply (car form) (cdr form))))
  1530.     (if (custom-facep face) face nil)))
  1531.  
  1532. (defun custom-const-insert (custom level)
  1533.   "Insert field for CUSTOM at nesting LEVEL in customization buffer."
  1534.   (let* ((field (custom-field-create custom custom-nil))
  1535.      (face (custom-field-face field))
  1536.      (from (point)))
  1537.     (custom-text-insert (custom-tag custom))
  1538.     (custom-add-text-properties from (point) 
  1539.              (list 'face face
  1540.                    rear-nonsticky t))
  1541.     (custom-documentation-insert custom)
  1542.     (custom-field-move field from (point))
  1543.     field))
  1544.  
  1545. (defun custom-const-update (field)
  1546.   "Update face of FIELD."
  1547.   (let ((from (custom-field-start field))
  1548.     (custom (custom-field-custom field)))
  1549.     (custom-put-text-property from (+ from (length (custom-tag custom)))
  1550.                'face (custom-field-face field))))
  1551.  
  1552. (defun custom-const-valid (custom value)
  1553.   "Non-nil if CUSTOM can validly have the value VALUE."
  1554.   (equal (custom-default custom) value))
  1555.  
  1556. (defun custom-const-face (field)
  1557.   "Face used for a FIELD."
  1558.   (custom-default (custom-field-custom field)))
  1559.  
  1560. (defun custom-sexp-read (custom string)
  1561.   "Read from CUSTOM an STRING."
  1562.   (save-match-data
  1563.     (save-excursion
  1564.       (set-buffer (get-buffer-create " *Custom Scratch*"))
  1565.       (erase-buffer)
  1566.       (insert string)
  1567.       (goto-char (point-min))
  1568.       (prog1 (read (current-buffer))
  1569.     (or (looking-at
  1570.          (concat (regexp-quote (char-to-string
  1571.                     (custom-padding custom)))
  1572.              "*\\'"))
  1573.         (error "Junk at end of expression"))))))
  1574.  
  1575. (autoload 'pp-to-string "pp")
  1576.  
  1577. (defun custom-sexp-write (custom sexp)
  1578.   "Write CUSTOM SEXP as string."
  1579.   (let ((string (prin1-to-string sexp)))
  1580.     (if (<= (length string) (custom-width custom))
  1581.     string
  1582.       (setq string (pp-to-string sexp))
  1583.       (string-match "[ \t\n]*\\'" string)
  1584.       (concat "\n" (substring string 0 (match-beginning 0))))))
  1585.  
  1586. (defun custom-string-read (custom string)
  1587.   "Read string by ignoring trailing padding characters."
  1588.   (let ((last (length string))
  1589.     (padding (custom-padding custom)))
  1590.     (while (and (> last 0)
  1591.         (eq (aref string (1- last)) padding))
  1592.       (setq last (1- last)))
  1593.     (substring string 0 last)))
  1594.  
  1595. (defun custom-string-write (custom string)
  1596.   "Write raw string."
  1597.   string)
  1598.  
  1599. (defun custom-button-insert (custom level)
  1600.   "Insert field for CUSTOM at nesting LEVEL in customization buffer."
  1601.   (custom-tag-insert (concat "[" (custom-tag custom) "]") 
  1602.              (custom-property custom 'query))
  1603.   (custom-documentation-insert custom)
  1604.   nil)
  1605.  
  1606. (defun custom-default-export (custom value)
  1607.   ;; Convert CUSTOM's VALUE to external representation.
  1608.   ;; See `custom-import'.
  1609.   (if (custom-eval custom value)
  1610.       (eval (car (custom-quote custom value)))
  1611.     value))
  1612.  
  1613. (defun custom-default-quote (custom value)
  1614.   "Quote CUSTOM's VALUE if necessary."
  1615.   (list (if (and (not (custom-eval custom value))
  1616.          (or (and (symbolp value)
  1617.               value 
  1618.               (not (eq t value)))
  1619.              (and (listp value)
  1620.               value
  1621.               (not (memq (car value) '(quote function lambda))))))
  1622.         (list 'quote value)
  1623.       value)))
  1624.  
  1625. (defun custom-default-initialize (custom)
  1626.   "Initialize `doc' and `default' entries in CUSTOM."
  1627.   (let ((name (custom-name custom)))
  1628.     (if (null name)
  1629.     ()
  1630.       (let ((default (custom-default custom))
  1631.         (doc (custom-documentation custom))
  1632.         (vdoc (documentation-property name 'variable-documentation t)))
  1633.     (if doc
  1634.         (or vdoc (put name 'variable-documentation doc))
  1635.       (if vdoc (custom-property-set custom 'doc vdoc)))
  1636.     (if (eq default custom-nil)
  1637.         (if (boundp name)
  1638.         (custom-property-set custom 'default (symbol-value name)))
  1639.       (or (boundp name)
  1640.           (set name default)))))))
  1641.  
  1642. (defun custom-default-insert (custom level)
  1643.   "Insert field for CUSTOM at nesting LEVEL in customization buffer."
  1644.   (let ((field (custom-field-create custom custom-nil))
  1645.     (tag (custom-tag custom)))
  1646.     (if (null tag)
  1647.     ()
  1648.       (custom-tag-insert tag field)
  1649.       (custom-text-insert ": "))
  1650.     (custom-field-insert field)
  1651.     (custom-documentation-insert custom)
  1652.     field))
  1653.  
  1654. (defun custom-default-accept (field value &optional original)
  1655.   "Store a new value into field FIELD, taking it from VALUE."
  1656.   (if original 
  1657.       (custom-field-original-set field value))
  1658.   (custom-field-value-set field value)
  1659.   (custom-field-update field))
  1660.   
  1661. (defun custom-default-apply (field)
  1662.   "Apply any changes in FIELD since the last apply."
  1663.   (let* ((custom (custom-field-custom field))
  1664.      (name (custom-name custom)))
  1665.     (if (null name)
  1666.     (error "This field cannot be applied alone"))
  1667.     (custom-external-set name (custom-name-value name))
  1668.     (custom-field-reset field)))
  1669.  
  1670. (defun custom-default-reset (field)
  1671.   "Reset content of editing FIELD to `original'."
  1672.   (custom-field-accept field (custom-field-original field) t))
  1673.  
  1674. (defun custom-default-factory-reset (field)
  1675.   "Reset content of editing FIELD to `default'."
  1676.   (let* ((custom (custom-field-custom field))
  1677.      (default (car (custom-import custom (custom-default custom)))))
  1678.     (or (eq default custom-nil)
  1679.     (custom-field-accept field default nil))))
  1680.  
  1681. (defun custom-default-query (field)
  1682.   "Prompt for a FIELD"
  1683.   (let* ((custom (custom-field-custom field))
  1684.      (value (custom-field-value field))
  1685.      (initial (custom-write custom value))
  1686.      (prompt (concat (custom-prompt custom) ": ")))
  1687.     (custom-field-accept field 
  1688.              (custom-read custom 
  1689.                       (if (custom-valid custom value)
  1690.                       (read-string prompt (cons initial 1))
  1691.                     (read-string prompt))))))
  1692.  
  1693. (defun custom-default-match (custom values)
  1694.   "Match CUSTOM with VALUES."
  1695.   values)
  1696.  
  1697. (defun custom-default-extract (custom field)
  1698.   "Extract CUSTOM's content in FIELD."
  1699.   (list (custom-field-value field)))
  1700.  
  1701. (defun custom-default-validate (custom field)
  1702.   "Validate FIELD."
  1703.   (let ((value (custom-field-value field))
  1704.     (start (custom-field-start field)))
  1705.     (cond ((eq value custom-nil)
  1706.        (cons start "Uninitialized field"))
  1707.       ((and (consp value) (eq (car value) custom-invalid))
  1708.        (cons start "Unparsable field content"))
  1709.       ((custom-valid custom value)
  1710.        nil)
  1711.       (t
  1712.        (cons start "Wrong type of field content")))))
  1713.  
  1714. (defun custom-default-face (field)
  1715.   "Face used for a FIELD."
  1716.   (let ((value (custom-field-value field)))
  1717.     (cond ((eq value custom-nil)
  1718.        custom-field-uninitialized-face)
  1719.       ((not (custom-valid (custom-field-custom field) value))
  1720.        custom-field-invalid-face)
  1721.       ((not (equal (custom-field-original field) value))
  1722.        custom-field-modified-face)
  1723.       (t
  1724.        custom-field-face))))
  1725.  
  1726. (defun custom-default-update (field)
  1727.   "Update the content of FIELD."
  1728.   (let ((inhibit-point-motion-hooks t)
  1729.     (before-change-functions nil)
  1730.     (after-change-functions nil)
  1731.     (start (custom-field-start field))
  1732.     (end (custom-field-end field)) 
  1733.     (pos (point)))
  1734.     ;; Keep track of how many modified fields we have.
  1735.     (cond ((equal (custom-field-value field) (custom-field-original field))
  1736.        (setq custom-modified-list (delq field custom-modified-list)))
  1737.       ((memq field custom-modified-list))
  1738.       (t
  1739.        (setq custom-modified-list (cons field custom-modified-list))))
  1740.     ;; Update the field.
  1741.     (goto-char end)
  1742.     (insert-before-markers " ")
  1743.     (delete-region start (1- end))
  1744.     (goto-char start)
  1745.     (custom-field-insert field)
  1746.     (goto-char end)
  1747.     (delete-char 1)
  1748.     (goto-char pos)
  1749.     (and (<= start pos) 
  1750.      (<= pos end)
  1751.      (custom-field-enter field))))
  1752.  
  1753. ;;; Create Buffer:
  1754. ;;
  1755. ;; Public functions to create a customization buffer and to insert
  1756. ;; various forms of text, fields, and buttons in it.
  1757.  
  1758. (defun customize ()
  1759.   "Customize GNU Emacs.
  1760. Create a *Customize* buffer with editable customization information
  1761. about GNU Emacs." 
  1762.   (interactive)
  1763.   (custom-buffer-create "*Customize*")
  1764.   (custom-reset-all))
  1765.  
  1766. (defun custom-buffer-create (name &optional custom types set get save)
  1767.   "Create a customization buffer named NAME.
  1768. If the optional argument CUSTOM is non-nil, use that as the custom declaration.
  1769. If the optional argument TYPES is non-nil, use that as the local types.
  1770. If the optional argument SET is non-nil, use that to set external data.
  1771. If the optional argument GET is non-nil, use that to get external data.
  1772. If the optional argument SAVE is non-nil, use that for saving changes."
  1773.   (switch-to-buffer name)
  1774.   (buffer-disable-undo (current-buffer))
  1775.   (custom-mode)
  1776.   (setq custom-local-type-properties types)
  1777.   (if (null custom)
  1778.       ()
  1779.     (make-local-variable 'custom-data)
  1780.     (setq custom-data custom))
  1781.   (if (null set)
  1782.       ()
  1783.     (make-local-variable 'custom-external-set)
  1784.     (setq custom-external-set set))
  1785.   (if (null get)
  1786.       ()
  1787.     (make-local-variable 'custom-external)
  1788.     (setq custom-external get))
  1789.   (if (null save)
  1790.       ()
  1791.     (make-local-variable 'custom-save)
  1792.     (setq custom-save save))
  1793.   (let ((inhibit-point-motion-hooks t)
  1794.     (before-change-functions nil)
  1795.     (after-change-functions nil))
  1796.     (erase-buffer)
  1797.     (insert "\n")
  1798.     (goto-char (point-min))
  1799.     (custom-text-insert "This is a customization buffer.\n")
  1800.     (custom-help-insert "\n")
  1801.     (custom-help-button 'custom-forward-field)
  1802.     (custom-help-button 'custom-backward-field)
  1803.     (custom-help-button 'custom-enter-value)
  1804.     (custom-help-button 'custom-field-factory-reset)
  1805.     (custom-help-button 'custom-field-reset)
  1806.     (custom-help-button 'custom-field-apply)
  1807.     (custom-help-button 'custom-save-and-exit)
  1808.     (custom-help-button 'custom-toggle-documentation)
  1809.     (custom-help-insert "\nClick mouse-2 on any button to activate it.\n")
  1810.     (custom-text-insert "\n")
  1811.     (custom-insert custom-data 0)
  1812.     (goto-char (point-min))))
  1813.  
  1814. (defun custom-insert (custom level)
  1815.   "Insert custom declaration CUSTOM in current buffer at level LEVEL."
  1816.   (if (stringp custom)
  1817.       (progn 
  1818.     (custom-text-insert custom)
  1819.     nil)
  1820.     (and level (null (custom-property custom 'header))
  1821.      (setq level nil))
  1822.     (and level 
  1823.      (> level 0)
  1824.      (custom-text-insert (concat "\n" (make-string level ?*) " ")))
  1825.     (let ((field (funcall (custom-property custom 'insert) custom level)))
  1826.       (custom-name-enter (custom-name custom) field)
  1827.       field)))
  1828.  
  1829. (defun custom-text-insert (text)
  1830.   "Insert TEXT in current buffer." 
  1831.   (insert text))
  1832.  
  1833. (defun custom-tag-insert (tag field &optional data)
  1834.   "Insert TAG for FIELD in current buffer."
  1835.   (let ((from (point)))
  1836.     (insert tag)
  1837.     (custom-category-set from (point) 'custom-button-properties)
  1838.     (custom-put-text-property from (point) 'custom-tag field)
  1839.     (if data
  1840.     (custom-add-text-properties from (point) (list 'custom-data data)))))
  1841.  
  1842. (defun custom-documentation-insert (custom &rest ignore)
  1843.   "Insert documentation from CUSTOM in current buffer."
  1844.   (let ((doc (custom-documentation custom)))
  1845.     (if (null doc)
  1846.     ()
  1847.       (custom-help-insert "\n" doc))))
  1848.  
  1849. (defun custom-help-insert (&rest args)
  1850.   "Insert ARGS as documentation text."
  1851.   (let ((from (point)))
  1852.     (apply 'insert args)
  1853.     (custom-category-set from (point) 'custom-documentation-properties)))
  1854.  
  1855. (defun custom-help-button (command)
  1856.   "Describe how to execute COMMAND."
  1857.   (let ((from (point)))
  1858.     (insert "`" (key-description (where-is-internal command nil t)) "'")
  1859.     (custom-set-text-properties from (point)
  1860.                 (list 'face custom-button-face
  1861.                       mouse-face custom-mouse-face
  1862.                       'custom-jump t ;Make TAB jump over it.
  1863.                       'custom-tag command
  1864.                       'start-open t
  1865.                       'end-open t))
  1866.     (custom-category-set from (point) 'custom-documentation-properties))
  1867.   (custom-help-insert ": " (custom-first-line (documentation command)) "\n"))
  1868.  
  1869. ;;; Mode:
  1870. ;;
  1871. ;; The Customization major mode and interactive commands. 
  1872.  
  1873. (defvar custom-mode-map nil
  1874.   "Keymap for Custom Mode.")
  1875. (if custom-mode-map
  1876.     nil
  1877.   (setq custom-mode-map (make-sparse-keymap))
  1878.   (define-key custom-mode-map (if (string-match "XEmacs" emacs-version) [button2] [mouse-2]) 'custom-push-button)
  1879.   (define-key custom-mode-map "\t" 'custom-forward-field)
  1880.   (define-key custom-mode-map "\M-\t" 'custom-backward-field)
  1881.   (define-key custom-mode-map "\r" 'custom-enter-value)
  1882.   (define-key custom-mode-map "\C-k" 'custom-kill-line)
  1883.   (define-key custom-mode-map "\C-c\C-r" 'custom-field-reset)
  1884.   (define-key custom-mode-map "\C-c\M-\C-r" 'custom-reset-all)
  1885.   (define-key custom-mode-map "\C-c\C-z" 'custom-field-factory-reset)
  1886.   (define-key custom-mode-map "\C-c\M-\C-z" 'custom-factory-reset-all)
  1887.   (define-key custom-mode-map "\C-c\C-a" 'custom-field-apply)
  1888.   (define-key custom-mode-map "\C-c\M-\C-a" 'custom-apply-all)
  1889.   (define-key custom-mode-map "\C-c\C-c" 'custom-save-and-exit)
  1890.   (define-key custom-mode-map "\C-c\C-d" 'custom-toggle-documentation))
  1891.  
  1892. ;; C-c keymap ideas: C-a field-beginning, C-e field-end, C-f
  1893. ;; forward-field, C-b backward-field, C-n next-field, C-p
  1894. ;; previous-field, ? describe-field.
  1895.  
  1896. (defun custom-mode ()
  1897.   "Major mode for doing customizations.
  1898.  
  1899. \\{custom-mode-map}"
  1900.   (kill-all-local-variables)
  1901.   (setq major-mode 'custom-mode
  1902.     mode-name "Custom")
  1903.   (use-local-map custom-mode-map)
  1904.   (make-local-variable 'before-change-functions)
  1905.   (setq before-change-functions '(custom-before-change))
  1906.   (make-local-variable 'after-change-functions)
  1907.   (setq after-change-functions '(custom-after-change))
  1908.   (if (not (fboundp 'make-local-hook))
  1909.       ;; Emacs 19.28 and earlier.
  1910.       (add-hook 'post-command-hook 
  1911.         (lambda ()
  1912.           (if (eq major-mode 'custom-mode)
  1913.               (custom-post-command))))
  1914.     ;; Emacs 19.29.
  1915.     (make-local-hook 'post-command-hook)
  1916.     (add-hook 'post-command-hook 'custom-post-command nil t)))
  1917.  
  1918. (defun custom-forward-field (arg)
  1919.   "Move point to the next field or button.
  1920. With optional ARG, move across that many fields."
  1921.   (interactive "p")
  1922.   (while (> arg 0)
  1923.     (let ((next (if (get-text-property (point) 'custom-tag)
  1924.             (next-single-property-change (point) 'custom-tag)
  1925.           (point))))
  1926.       (setq next (or (next-single-property-change next 'custom-tag)
  1927.              (next-single-property-change (point-min) 'custom-tag)))
  1928.       (if next
  1929.       (goto-char next)
  1930.     (error "No customization fields in this buffer.")))
  1931.     (or (get-text-property (point) 'custom-jump)
  1932.     (setq arg (1- arg))))
  1933.   (while (< arg 0)
  1934.     (let ((previous (if (get-text-property (1- (point)) 'custom-tag)
  1935.             (previous-single-property-change (point) 'custom-tag)
  1936.               (point))))
  1937.       (setq previous
  1938.         (or (previous-single-property-change previous 'custom-tag)
  1939.         (previous-single-property-change (point-max) 'custom-tag)))
  1940.       (if previous
  1941.       (goto-char previous)
  1942.     (error "No customization fields in this buffer.")))
  1943.     (or (get-text-property (1- (point)) 'custom-jump)
  1944.     (setq arg (1+ arg)))))
  1945.  
  1946. (defun custom-backward-field (arg)
  1947.   "Move point to the previous field or button.
  1948. With optional ARG, move across that many fields."
  1949.   (interactive "p")
  1950.   (custom-forward-field (- arg)))
  1951.  
  1952. (defun custom-toggle-documentation (&optional arg)
  1953.   "Toggle display of documentation text.
  1954. If the optional argument is non-nil, show text iff the argument is positive."
  1955.   (interactive "P")
  1956.   (let ((hide (or (and (null arg) 
  1957.                (null (custom-category-get 
  1958.                   'custom-documentation-properties 'invisible)))
  1959.           (<= (prefix-numeric-value arg) 0))))
  1960.     (custom-category-put 'custom-documentation-properties 'invisible hide)
  1961.     (custom-category-put 'custom-documentation-properties intangible hide))
  1962.   (redraw-display))
  1963.  
  1964. (defun custom-enter-value (field data)
  1965.   "Enter value for current customization field or push button."
  1966.   (interactive (list (get-text-property (point) 'custom-tag)
  1967.              (get-text-property (point) 'custom-data)))
  1968.   (cond (data
  1969.      (funcall field data))
  1970.     ((eq field 'custom-enter-value)
  1971.      (error "Don't be silly"))
  1972.     ((and (symbolp field) (fboundp field))
  1973.      (call-interactively field))
  1974.     (field
  1975.      (custom-field-query field))
  1976.     (t
  1977.      (message "Nothing to enter here"))))
  1978.  
  1979. (defun custom-kill-line ()
  1980.   "Kill to end of field or end of line, whichever is first."
  1981.   (interactive)
  1982.   (let ((field (get-text-property (point) 'custom-field))
  1983.     (newline (save-excursion (search-forward "\n")))
  1984.     (next (next-single-property-change (point) 'custom-field)))
  1985.     (if (and field (> newline next))
  1986.     (kill-region (point) next)
  1987.       (call-interactively 'kill-line))))
  1988.  
  1989. (defun custom-push-button (event)
  1990.   "Activate button below mouse pointer."
  1991.   (interactive "@e")
  1992.   (let* ((pos (event-point event))
  1993.          (field (get-text-property pos 'custom-field))
  1994.          (tag (get-text-property pos 'custom-tag))
  1995.      (data (get-text-property pos 'custom-data)))
  1996.     (cond (data
  1997.         (funcall tag data))
  1998.       ((and (symbolp tag) (fboundp tag))
  1999.        (call-interactively tag))
  2000.       (field
  2001.        (call-interactively (lookup-key global-map (this-command-keys))))
  2002.       (tag
  2003.        (custom-enter-value tag data))
  2004.       (t 
  2005.        (error "Nothing to click on here.")))))
  2006.  
  2007. (defun custom-reset-all ()
  2008.   "Undo any changes since the last apply in all fields."
  2009.   (interactive (and custom-modified-list
  2010.             (not (y-or-n-p "Discard all changes? "))
  2011.             (error "Reset aborted")))
  2012.   (let ((all custom-name-fields)
  2013.     current field)
  2014.     (while all
  2015.       (setq current (car all)
  2016.         field (cdr current)
  2017.         all (cdr all))
  2018.       (custom-field-reset field))))
  2019.  
  2020. (defun custom-field-reset (field)
  2021.   "Undo any changes in FIELD since the last apply."
  2022.   (interactive (list (or (get-text-property (point) 'custom-field)
  2023.              (get-text-property (point) 'custom-tag))))
  2024.   (if (arrayp field)
  2025.       (let* ((custom (custom-field-custom field))
  2026.          (name (custom-name custom)))
  2027.     (save-excursion
  2028.       (if name
  2029.           (custom-field-original-set 
  2030.            field (car (custom-import custom (custom-external name)))))
  2031.       (if (not (custom-valid custom (custom-field-original field)))
  2032.           (error "This field cannot be reset alone")
  2033.         (funcall (custom-property custom 'reset) field)
  2034.         (funcall (custom-property custom 'synchronize) field))))))
  2035.  
  2036. (defun custom-factory-reset-all ()
  2037.   "Reset all field to their default values."
  2038.   (interactive (and custom-modified-list
  2039.             (not (y-or-n-p "Discard all changes? "))
  2040.             (error "Reset aborted")))
  2041.   (let ((all custom-name-fields)
  2042.     field)
  2043.     (while all
  2044.       (setq field (cdr (car all))
  2045.         all (cdr all))
  2046.       (custom-field-factory-reset field))))
  2047.  
  2048. (defun custom-field-factory-reset (field)
  2049.   "Reset FIELD to its default value."
  2050.   (interactive (list (or (get-text-property (point) 'custom-field)
  2051.              (get-text-property (point) 'custom-tag))))
  2052.   (if (arrayp field)
  2053.       (save-excursion
  2054.     (funcall (custom-property (custom-field-custom field) 'factory-reset)
  2055.          field))))
  2056.  
  2057. (defun custom-apply-all ()
  2058.   "Apply any changes since the last reset in all fields."
  2059.   (interactive (if custom-modified-list
  2060.            nil
  2061.          (error "No changes to apply.")))
  2062.   (custom-field-parse custom-field-last)
  2063.   (let ((all custom-name-fields)
  2064.     field)
  2065.     (while all
  2066.       (setq field (cdr (car all))
  2067.         all (cdr all))
  2068.       (let ((error (custom-field-validate (custom-field-custom field) field)))
  2069.     (if (null error)
  2070.         ()
  2071.       (goto-char (car error))
  2072.       (error (cdr error))))))
  2073.   (let ((all custom-name-fields)
  2074.     field)
  2075.     (while all
  2076.       (setq field (cdr (car all))
  2077.         all (cdr all))
  2078.       (custom-field-apply field))))
  2079.  
  2080. (defun custom-field-apply (field)
  2081.   "Apply any changes in FIELD since the last apply."
  2082.   (interactive (list (or (get-text-property (point) 'custom-field)
  2083.              (get-text-property (point) 'custom-tag))))
  2084.   (custom-field-parse custom-field-last)
  2085.   (if (arrayp field)
  2086.       (let* ((custom (custom-field-custom field))
  2087.          (error (custom-field-validate custom field)))
  2088.     (if error
  2089.         (error (cdr error)))
  2090.     (funcall (custom-property custom 'apply) field))))
  2091.  
  2092. (defun custom-toggle-hide (&rest ignore)
  2093.   "Hide or show entry."
  2094.   (interactive)
  2095.   (error "This button is not yet implemented"))
  2096.  
  2097. (defun custom-save-and-exit ()
  2098.   "Save and exit customization buffer."
  2099.   (interactive "@")
  2100.   (save-excursion
  2101.    (funcall custom-save))
  2102.   (kill-buffer (current-buffer)))
  2103.  
  2104. (defun custom-save ()
  2105.   "Save customization information."
  2106.   (interactive)
  2107.   (custom-apply-all)
  2108.   (let ((new custom-name-fields))
  2109.     (set-buffer (find-file-noselect custom-file))
  2110.     (goto-char (point-min))
  2111.     (save-excursion
  2112.       (let ((old (condition-case nil
  2113.              (read (current-buffer))
  2114.            (end-of-file (append '(setq custom-dummy
  2115.                            'custom-dummy) ())))))
  2116.     (or (eq (car old) 'setq)
  2117.         (error "Invalid customization file: %s" custom-file))
  2118.     (while new
  2119.       (let* ((field (cdr (car new)))
  2120.          (custom (custom-field-custom field))
  2121.          (value (custom-field-original field))
  2122.          (default (car (custom-import custom (custom-default custom))))
  2123.          (name (car (car new))))
  2124.         (setq new (cdr new))
  2125.         (custom-assert '(eq name (custom-name custom)))
  2126.         (if (equal default value)
  2127.         (setcdr old (custom-plist-delq name (cdr old)))
  2128.           (setcdr old (plist-put (cdr old) name 
  2129.                      (car (custom-quote custom value)))))))
  2130.     (erase-buffer)
  2131.     (insert ";; " custom-file "\
  2132.  --- Automatically generated customization information.
  2133. ;; 
  2134. ;; Feel free to edit by hand, but the entire content should consist of
  2135. ;; a single setq.  Any other lisp expressions will confuse the
  2136. ;; automatic configuration engine.
  2137.  
  2138. \(setq ")
  2139.     (setq old (cdr old))
  2140.     (while old
  2141.       (prin1 (car old) (current-buffer))
  2142.       (setq old (cdr old))
  2143.       (insert " ")
  2144.       (pp (car old) (current-buffer))
  2145.       (setq old (cdr old))
  2146.       (if old (insert "\n      ")))
  2147.     (insert ")\n")
  2148.     (save-buffer)
  2149.     (kill-buffer (current-buffer))))))
  2150.  
  2151. (defun custom-load ()
  2152.   "Save customization information."
  2153.   (interactive (and custom-modified-list
  2154.             (not (equal (list (custom-name-field 'custom-file))
  2155.                 custom-modified-list))
  2156.             (not (y-or-n-p "Discard all changes? "))
  2157.             (error "Load aborted")))
  2158.   (load-file (custom-name-value 'custom-file))
  2159.   (custom-reset-all))
  2160.  
  2161. ;;; Field Editing:
  2162. ;;
  2163. ;; Various internal functions for implementing the direct editing of
  2164. ;; fields in the customization buffer.
  2165.  
  2166. (defun custom-field-untouch (field)
  2167.   ;; Remove FIELD and its children from `custom-modified-list'.
  2168.   (setq custom-modified-list (delq field custom-modified-list))
  2169.   (if (arrayp field)
  2170.       (let ((value (custom-field-value field)))
  2171.     (cond ((null (custom-data (custom-field-custom field))))
  2172.           ((arrayp value)
  2173.            (custom-field-untouch value))
  2174.           ((listp value)
  2175.            (mapcar 'custom-field-untouch value))))))
  2176.  
  2177.  
  2178. (defun custom-field-insert (field)
  2179.   ;; Insert editing FIELD in current buffer.
  2180.   (let ((from (point))
  2181.     (custom (custom-field-custom field))
  2182.     (value (custom-field-value field)))
  2183.     (insert (custom-write custom value))
  2184.     (insert-char (custom-padding custom)
  2185.          (- (custom-width custom) (- (point) from)))
  2186.     (custom-field-move field from (point))
  2187.     (custom-set-text-properties 
  2188.      from (point)
  2189.      (list 'custom-field field
  2190.        'custom-tag field
  2191.        'face (custom-field-face field)
  2192.        'start-open t
  2193.        'end-open t))))
  2194.  
  2195. (defun custom-field-read (field)
  2196.   ;; Read the screen content of FIELD.
  2197.   (custom-read (custom-field-custom field)
  2198.            (custom-buffer-substring-no-properties (custom-field-start field)
  2199.                            (custom-field-end field))))
  2200.  
  2201. ;; Fields are shown in a special `active' face when point is inside
  2202. ;; it.  You activate the field by moving point inside (entering) it
  2203. ;; and deactivate the field by moving point outside (leaving) it.
  2204.  
  2205. (defun custom-field-leave (field)
  2206.   ;; Deactivate FIELD.
  2207.   (let ((before-change-functions nil)
  2208.     (after-change-functions nil))
  2209.     (custom-put-text-property (custom-field-start field) (custom-field-end field)
  2210.                'face (custom-field-face field))))
  2211.  
  2212. (defun custom-field-enter (field)
  2213.   ;; Activate FIELD.
  2214.   (let* ((start (custom-field-start field)) 
  2215.      (end (custom-field-end field))
  2216.      (custom (custom-field-custom field))
  2217.      (padding (custom-padding custom))
  2218.      (before-change-functions nil)
  2219.      (after-change-functions nil))
  2220.     (or (eq this-command 'self-insert-command)
  2221.     (let ((pos end))
  2222.       (while (and (< start pos)
  2223.               (eq (char-after (1- pos)) padding))
  2224.         (setq pos (1- pos)))
  2225.       (if (< pos (point))
  2226.           (goto-char pos))))
  2227.     (custom-put-text-property start end 'face custom-field-active-face)))
  2228.  
  2229. (defun custom-field-resize (field)
  2230.   ;; Resize FIELD after change.
  2231.   (let* ((custom (custom-field-custom field))
  2232.      (begin (custom-field-start field))
  2233.      (end (custom-field-end field))
  2234.      (pos (point))
  2235.      (padding (custom-padding custom))
  2236.      (width (custom-width custom))
  2237.      (size (- end begin)))
  2238.     (cond ((< size width)
  2239.        (goto-char end)
  2240.        (if (fboundp 'insert-before-markers-and-inherit)
  2241.            ;; Emacs 19.
  2242.            (insert-before-markers-and-inherit
  2243.         (make-string (- width size) padding))
  2244.          ;; XEmacs:  BUG:  Doesn't work!
  2245.          (insert-before-markers (make-string (- width size) padding)))
  2246.        (goto-char pos))
  2247.       ((> size width)
  2248.        (let ((start (if (and (< (+ begin width) pos) (<= pos end))
  2249.                 pos
  2250.               (+ begin width))))
  2251.          (goto-char end)
  2252.          (while (and (< start (point)) (= (preceding-char) padding))
  2253.            (backward-delete-char 1))
  2254.          (goto-char pos))))))
  2255.  
  2256. (defvar custom-field-changed nil)
  2257. ;; List of fields changed on the screen but whose VALUE attribute has
  2258. ;; not yet been updated to reflect the new screen content.
  2259. (make-variable-buffer-local 'custom-field-changed)
  2260.  
  2261. (defun custom-field-parse (field)
  2262.   ;; Parse FIELD content iff changed.
  2263.   (if (memq field custom-field-changed)
  2264.       (progn 
  2265.     (setq custom-field-changed (delq field custom-field-changed))
  2266.     (custom-field-value-set field (custom-field-read field))
  2267.     (custom-field-update field))))
  2268.  
  2269. (defun custom-post-command ()
  2270.   ;; Keep track of their active field.
  2271.   (custom-assert '(eq major-mode 'custom-mode))
  2272.   (let ((field (custom-field-property (point))))
  2273.     (if (eq field custom-field-last)
  2274.     (if (memq field custom-field-changed)
  2275.         (custom-field-resize field))
  2276.       (custom-field-parse custom-field-last)
  2277.       (if custom-field-last
  2278.       (custom-field-leave custom-field-last))
  2279.       (if field
  2280.       (custom-field-enter field))
  2281.       (setq custom-field-last field))
  2282.     (set-buffer-modified-p (or custom-modified-list
  2283.                    custom-field-changed))))
  2284.  
  2285. (defvar custom-field-was nil)
  2286. ;; The custom data before the change.
  2287. (make-variable-buffer-local 'custom-field-was)
  2288.  
  2289. (defun custom-before-change (begin end)
  2290.   ;; Check that we the modification is allowed.
  2291.   (if (not (eq major-mode 'custom-mode))
  2292.       (message "Aargh! Why is custom-before-change called here?")
  2293.     (let ((from (custom-field-property begin))
  2294.       (to (custom-field-property end)))
  2295.       (cond ((or (null from) (null to))
  2296.          (error "You can only modify the fields"))
  2297.         ((not (eq from to))
  2298.          (error "Changes must be limited to a single field."))
  2299.         (t
  2300.          (setq custom-field-was from))))))
  2301.  
  2302. (defun custom-after-change (begin end length)
  2303.   ;; Keep track of field content.
  2304.   (if (not (eq major-mode 'custom-mode))
  2305.       (message "Aargh! Why is custom-after-change called here?")
  2306.     (let ((field custom-field-was))
  2307.       (custom-assert '(prog1 field (setq custom-field-was nil)))
  2308.       ;; Prevent mixing fields properties.
  2309.       (custom-put-text-property begin end 'custom-field field)
  2310.       ;; Update the field after modification.
  2311.       (if (eq (custom-field-property begin) field)
  2312.       (let ((field-end (custom-field-end field)))
  2313.         (if (> end field-end)
  2314.         (set-marker field-end end))
  2315.         (add-to-list 'custom-field-changed field))
  2316.     ;; We deleted the entire field, reinsert it.
  2317.     (custom-assert '(eq begin end))
  2318.     (save-excursion
  2319.       (goto-char begin)
  2320.       (custom-field-value-set field
  2321.                   (custom-read (custom-field-custom field) ""))
  2322.       (custom-field-insert field))))))
  2323.  
  2324. (defun custom-field-property (pos)
  2325.   ;; The `custom-field' text property valid for POS.
  2326.   (or (get-text-property pos 'custom-field)
  2327.       (and (not (eq pos (point-min)))
  2328.        (get-text-property (1- pos) 'custom-field))))
  2329.  
  2330. ;;; Generic Utilities:
  2331. ;;
  2332. ;; Some utility functions that are not really specific to custom.
  2333.  
  2334. (defun custom-assert (expr)
  2335.   "Assert that EXPR evaluates to non-nil at this point"
  2336.   (or (eval expr)
  2337.       (error "Assertion failed: %S" expr)))
  2338.  
  2339. (defun custom-first-line (string)
  2340.   "Return the part of STRING before the first newline."
  2341.   (let ((pos 0)
  2342.     (len (length string)))
  2343.     (while (and (< pos len) (not (eq (aref string pos) ?\n)))
  2344.       (setq pos (1+ pos)))
  2345.     (if (eq pos len)
  2346.     string
  2347.     (substring string 0 pos))))
  2348.  
  2349. (defun custom-insert-before (list old new)
  2350.   "In LIST insert before OLD a NEW element."
  2351.   (cond ((null list)
  2352.      (list new))
  2353.     ((null old)
  2354.      (nconc list (list new)))
  2355.     ((eq old (car list))
  2356.      (cons new list))
  2357.     (t
  2358.      (let ((list list))
  2359.        (while (not (eq old (car (cdr list))))
  2360.          (setq list (cdr list))
  2361.          (custom-assert '(cdr list)))
  2362.        (setcdr list (cons new (cdr list))))
  2363.      list)))
  2364.  
  2365. (defun custom-strip-padding (string padding)
  2366.   "Remove padding from STRING."
  2367.   (let ((regexp (concat (regexp-quote (char-to-string padding)) "+")))
  2368.     (while (string-match regexp string)
  2369.       (setq string (concat (substring string 0 (match-beginning 0))
  2370.                (substring string (match-end 0))))))
  2371.   string)
  2372.  
  2373. (defun custom-plist-memq (prop plist)
  2374.   "Return non-nil if PROP is a property of PLIST.  Comparison done with EQ."
  2375.   (let (result)
  2376.     (while plist
  2377.       (if (eq (car plist) prop)
  2378.       (setq result plist
  2379.         plist nil)
  2380.     (setq plist (cdr (cdr plist)))))
  2381.     result))
  2382.  
  2383. (defun custom-plist-delq (prop plist)
  2384.   "Delete property PROP from property list PLIST."
  2385.   (while (eq (car plist) prop)
  2386.     (setq plist (cdr (cdr plist))))
  2387.   (let ((list plist)
  2388.     (next (cdr (cdr plist))))
  2389.     (while next
  2390.       (if (eq (car next) prop)
  2391.       (progn 
  2392.         (setq next (cdr (cdr next)))
  2393.         (setcdr (cdr list) next))
  2394.     (setq list next
  2395.           next (cdr (cdr next))))))
  2396.   plist)
  2397.  
  2398. ;;; Meta Customization:
  2399.  
  2400. (custom-declare '()
  2401.   '((tag . "Meta Customization")
  2402.     (doc . "Customization of the customization support.")
  2403.     (type . group)
  2404.     (data ((type . face-doc))
  2405.       ((tag . "Button Face")
  2406.        (default . bold)
  2407.        (doc . "Face used for tags in customization buffers.")
  2408.        (name . custom-button-face)
  2409.        (synchronize . (lambda (f)
  2410.                 (custom-category-put 'custom-button-properties 
  2411.                          'face custom-button-face)))
  2412.        (type . face))
  2413.       ((tag . "Mouse Face")
  2414.        (default . highlight)
  2415.        (doc . "\
  2416. Face used when mouse is above a button in customization buffers.")
  2417.        (name . custom-mouse-face)
  2418.        (synchronize . (lambda (f)
  2419.                 (custom-category-put 'custom-button-properties 
  2420.                          mouse-face 
  2421.                          custom-mouse-face)))
  2422.        (type . face))
  2423.       ((tag . "Field Face")
  2424.        (default . italic)
  2425.        (doc . "Face used for customization fields.")
  2426.        (name . custom-field-face)
  2427.        (type . face))
  2428.       ((tag . "Uninitialized Face")
  2429.        (default . modeline)
  2430.        (doc . "Face used for uninitialized customization fields.")
  2431.        (name . custom-field-uninitialized-face)
  2432.        (type . face))
  2433.       ((tag . "Invalid Face")
  2434.        (default . highlight)
  2435.        (doc . "\
  2436. Face used for customization fields containing invalid data.")
  2437.        (name . custom-field-invalid-face)
  2438.        (type . face))
  2439.       ((tag . "Modified Face")
  2440.        (default . bold-italic)
  2441.        (doc . "Face used for modified customization fields.")
  2442.        (name . custom-field-modified-face)
  2443.        (type . face))
  2444.       ((tag . "Active Face")
  2445.        (default . underline)
  2446.        (doc . "\
  2447. Face used for customization fields while they are being edited.")
  2448.        (name . custom-field-active-face)
  2449.        (type . face)))))
  2450.  
  2451. ;; custom.el uses two categories.
  2452.  
  2453. (custom-category-create 'custom-documentation-properties)
  2454. (custom-category-put 'custom-documentation-properties rear-nonsticky t)
  2455.  
  2456. (custom-category-create 'custom-button-properties)
  2457. (custom-category-put 'custom-button-properties 'face custom-button-face)
  2458. (custom-category-put 'custom-button-properties mouse-face custom-mouse-face)
  2459. (custom-category-put 'custom-button-properties rear-nonsticky t)
  2460.  
  2461. (custom-category-create 'custom-hidden-properties)
  2462. (custom-category-put 'custom-hidden-properties 'invisible
  2463.              (not (string-match "XEmacs" emacs-version)))
  2464. (custom-category-put 'custom-hidden-properties intangible t)
  2465.  
  2466. (if (file-readable-p custom-file)
  2467.     (load-file custom-file))
  2468.  
  2469. (provide 'custom)
  2470.  
  2471. ;;; custom.el ends here
  2472.